get_json_object
The get_json_object function is used to parse a JSON object based on the given path. If the JSON object is invalid, NULL is returned.
Syntax
get_json_object( json_string , path )
Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| json_string | Yes | STRING | A standard JSON format object, in the format {Key:Value, Key:Value,...}. |
| path | Yes | STRING | A JSONPath expression used to specify the location of the element to extract. The meanings of different characters are as follows:
|
Return Values
Returns a value of type STRING.
- If json is empty or in an invalid JSON format, NULL is returned.
- If json is valid and path exists, the corresponding string is returned.
Example Code
- Extract information from the JSON object src_json.json. Example data is as follows.
jsonString = {"store": {"fruit":[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} }, "email":"amy@only_for_json_udf_test.net", "owner":"Tony" }Extract the owner field information from the above example. Return Tony.
select get_json_object(jsonString, '$.owner');
Extract the first array information from the store.fruit field. Return {"weight":8,"type":"apple"}.
select get_json_object(jsonString, '$.store.fruit[0]');
Extract information from a non-existent field. Return NULL.
select get_json_object(jsonString, '$.non_exist_key');
- Extract information from an array-type JSON object. The command example is as follows.
select get_json_object('{"array":[["a",11],["b",22],["c",33]]}','$.array[1][1]');Returns ["h00","h11","h22"].
select get_json_object('{"a":"b","c":{"d":"e","f":"g","h":["h00","h11","h22"]},"i":"j"}','$.c.h[*]');Returns ["h00","h11","h22"].
select get_json_object('{"a":"b","c":{"d":"e","f":"g","h":["h00","h11","h22"]},"i":"j"}','$.c.h');Returns h11.
select get_json_object('{"a":"b","c":{"d":"e","f":"g","h":["h00","h11","h22"]},"i":"j"}','$.c.h[1]');
- Extract information from a JSON object containing .. The command example is as follows.
Create a table.
create table json_table (id string, json string);
Insert data into the table. The key contains ..
insert into table json_table (id, json) values ("1", "{\"city1\":{\"region\":{\"rid\":6}}}");Insert data into the table. The key does not contain ..
insert into table json_table (id, json) values ("2", "{\"city1\":{\"region\":{\"rid\":7}}}");Get the value of rid and query the key as city1. Return 6. Since it contains ., it can only be parsed using [''].
select get_json_object(json, "$['city1'].region['rid']") from json_table where id =1;
Get the value of rid and query the key as city1. Return 7. There are two query methods.
select get_json_object(json, "$['city1'].region['rid']") from json_table where id =2; select get_json_object(json, "$.city1.region['rid']") from json_table where id =2;
- JSON input is empty or in an invalid format. The command example is as follows.
select get_json_object('','$.array[2]');Returns NULL.
select get_json_object('"array":["a",1],"b":["c",3]','$.array[1][1]');
- JSON string involves escape characters. The command example is as follows.
select get_json_object('{"a":"\\"3\\"","b":"6"}', '$.a');Returns 3.
select get_json_object('{"a":"\'3\'","b":"6"}', '$.a');
- A JSON object can contain the same Key, and it can be parsed successfully.
select get_json_object('{"b":"1","b":"2"}', '$.b');
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot