问题
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