如何从json列表中提取内容?

时间:2017-06-12 07:48:53

标签: sql hive hiveql

有一个很长的string值,其中包含json列表:

{"name":"jack","age":"38","city":"JP"},{"name":"lee","age":"42","city":"tjs"},{"name":"smith","age":"46","city":"kh"}

目标是提取name的信息,因此结果为'jack,lee,smith'

我尝试get_json_object,但它返回null;我还尝试使用get_json_object split,但仍未使用...

Hive中是否有可以实现此需求的合适功能?

1 个答案:

答案 0 :(得分:1)

with t as (select '{"name":"jack","age":"38","city":"JP"},{"name":"lee","age":"42","city":"tjs"},{"name":"smith","age":"46","city":"kh"}' as myjson)
select  get_json_object(concat('{"x":[',myjson,']}'),'$.x.name[*]')  as names
from    t
+------------------------+
|         names          |
+------------------------+
| ["jack","lee","smith"] |
+------------------------+
with t as (select '{"name":"jack","age":"38","city":"JP"},{"name":"lee","age":"42","city":"tjs"},{"name":"smith","age":"46","city":"kh"}' as myjson)
select  translate(get_json_object(concat('{"x":[',myjson,']}'),'$.x.name[*]'),'[]"','')  as names
from    t
+----------------+
|     names      |
+----------------+
| jack,lee,smith |
+----------------+