test_table 表中 json_str 字符串格式如下
[
{
"id":1,
"name":"a"
},
{
"id":2,
"name":"b"
}
]
SQL解析方式为
SELECT get_json_object(test_json,'$.id') AS id,get_json_object(test_json,'$.name') AS id
FROM test_table
LATERAL VIEW explode(split(regexp_replace(regexp_replace(json_str,'\\[|\\]',''),'\\},\\{','\\}@\\{'),'\\@')) json AS test_json
上面SQL中,最内层 regexp_replace
的作用是匹配去除左右中括号。外层regexp_replace
的作用是转义数组中,json对象间连接的,
。这里选用@
作为匹配替换的符号,是因为表中,这个json字符串不会出现这个字符。最后,使用split
将各个json对象分开,LATERAL VIEW explode
展开这些对象,get_json_object
顺利从对象中取值。
参考网址
一文学会Hive解析Json数组