案例json_str
{
"item": {
"tabid": "新人专享",
"channelid": "APP",
"ass_rule": [{
"lv1": "ass",
"lv2": "mpp"
}]
},
"name": "99美妆节"
}
一、hive解析get_json_object
语法:get_json_object(string json_string, string path) → varchar
(1)get单层json
hive> select get_json_object(json_str, '$.name');
hive> 99美妆节
(2)get多层json
hive> select get_json_object(json_str, '$.item.tabid');
hive> 新人专享
(3)get json数组
hive> select get_json_object(json_str, '$.item.ass_rule[0]');
hive> {"lv1":"ass","lv2":"mpp"}
二、presto解析json_extract_scalar
语法:json_extract_scalar(string json, string json_path) → varchar
语法:json_extract(string json, string json_path) → json
(1)get单层json
hive> select json_extract_scalar(json_str, '$.name');
hive> 99美妆节
(2)get多层json
hive> select json_extract_scalar(json_str, '$.item.tabid');
hive> 新人专享
(3)get json数组
注意这里用的是json_extract
hive> select json_extract(json_str, '$.item.ass_rule[0]');
hive> {"lv1":"ass","lv2":"mpp"}