explode_json_object
功能描述
explode_json_object 表函数,将 JSON 对象展开为多行,每行包含一个键值对。 通常用于将 JSON 对象展开为更易查询的格式。该函数只支持包含元素的 JSON 对象
。需配合 LATERAL VIEW 使用。
语法
EXPLODE_JSON_OBJECT(<json>)
参数说明
<json> JSON 类型,其内容应该是 JSON 对象。
返回结果
- 返回由
<json> 所有元素组成的单列多行数据,列类型为 Nullable<Struct<String, JSON>>。
- 如果
<json> 为 NULL 或者不是 JSON 对象(比如是数组 [])返回 0 行数据。
- 如果
<json> 为空对象(比如 {}),返回 0 行数据。
使用示例
CREATE TABLE test_explode_json(id int, j json);
INSERT INTO test_explode_json VALUES
(1, parse_json('{"a": 1, "b": "hello", "c": [1,2,3]}')),
(2, parse_json('[1, 2, "three", 4.5, null]')),
(3, parse_json('null')),
(4, null),
(5, parse_json('[]')),
(6, parse_json('{}')),
(7, parse_json('[{"x":1},{"y":2}]')),
(8, parse_json('{"nested": {"a": 1}, "arr": [1,2]}'));
SELECT id, key, value FROM test_explode_json LATERAL VIEW OUTER explode_json_object(j) lv AS key, value ORDER BY id, key;
