EXPLODE_JSON_OBJECT
Description
The explode_json_object
table function expands a JSON object into multiple rows, each containing a key-value pair.
It is commonly used to transform JSON objects into a more query-friendly format. This function only supports JSON objects with elements.
It should be used together with LATERAL VIEW
.
Syntax
EXPLODE_JSON_OBJECT(<json>)
Parameters
<json>
JSON type, the content should be a JSON object.
Return Value
- Returns a single-column, multi-row result composed of all elements in
<json>
. The column type isNullable<Struct<String, JSON>>
. - If
<json>
is NULL or not a JSON object (such as an array[]
), 0 rows are returned. - If
<json>
is an empty object (such as{}
), 0 rows are returned.
Examples
- Prepare data
create table example(
k1 int
) properties(
"replication_num" = "1"
);
insert into example values(1); - Regular parameters
select * from example lateral view explode_json_object('{"k1": "v1", "k2": 123}') t2 as c;
+------+------------------------------+
| k1 | c |
+------+------------------------------+
| 1 | {"col1":"k1", "col2":""v1""} |
| 1 | {"col1":"k2", "col2":"123"} |
+------+------------------------------+ - Expand key-value pairs into separate columns
select * from example lateral view explode_json_object('{"k1": "v1", "k2": 123}') t2 as k, v;
+------+------+------+
| k1 | k | v |
+------+------+------+
| 1 | k1 | "v1" |
| 1 | k2 | 123 |
+------+------+------+The type of
v
is JSON - Empty object
select * from example lateral view explode_json_object('{}') t2 as c;
Empty set (0.03 sec)
- NULL parameter
select * from example lateral view explode_json_object(NULL) t2 as c;
Empty set (0.03 sec)
- Non-object parameter
select * from example lateral view explode_json_object('[]') t2 as c;
Empty set (0.03 sec)