Extract nested nested JSON array in Presto

这一生的挚爱 提交于 2020-05-28 07:26:26

问题


Say I have a JSON object that looks like this:

{"attributes":{"blah":"bleh","transactionlist":[{"ids":["a","b","c","d"]}]}}

I've attempted to extract the ids (a,b,c,d) into rows in Presto. From looking at other resources, it seems I should be casting the "ids" element into a map and then array, and unnest eventually. However, I am having some trouble doing this as the "ids" element is nested within a nested element. Anyone have any tips?

Thanks!


回答1:


Since the ids element in an JSON array nested in another JSON array, you need to UNNEST twice:

presto> SELECT id
     -> FROM (VALUES (JSON '{"attributes":{"blah":"bleh","transactionlist":[{"ids":["a","b","c","d"]}]}}')) t(x)
     -> CROSS JOIN UNNEST (CAST(json_extract(x, '$.attributes.transactionlist') AS ARRAY<JSON>)) u(transaction)
     -> CROSS JOIN UNNEST (CAST(json_extract(transaction, '$.ids') AS ARRAY<varchar>)) z(id)
     -> ;
 id
----
 a
 b
 c
 d
(4 rows)


来源:https://stackoverflow.com/questions/52600427/extract-nested-nested-json-array-in-presto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!