在Presto中提取嵌套的嵌套JSON数组

时间:2018-10-02 00:06:08

标签: arrays json presto

说我有一个看起来像这样的JSON对象:

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

我试图将ID(a,b,c,d)提取到Presto中的行中。通过查看其他资源,似乎我应该将“ ids”元素转换为映射,然后转换为数组,并最终嵌套。但是,由于“ ids”元素嵌套在嵌套元素中,因此我在执行此操作时遇到了一些麻烦。有人有提示吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

由于JSON数组中的ids元素嵌套在另一个JSON数组中,因此您需要两次UNNEST

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)