在 Presto 中提取嵌套嵌套 JSON 数组
Extract nested nested JSON array in Presto
假设我有一个如下所示的 JSON 对象:
{"attributes":{"blah":"bleh","transactionlist":[{"ids":["a","b","c","d"]}]}}
我试图将 ID (a,b,c,d) 提取到 Presto 中的行中。从查看其他资源来看,我似乎应该将 "ids" 元素转换为映射,然后转换为数组,最后取消嵌套。但是,我在执行此操作时遇到了一些麻烦,因为 "ids" 元素嵌套在嵌套元素中。有人有任何提示吗?
谢谢!
由于 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)
假设我有一个如下所示的 JSON 对象:
{"attributes":{"blah":"bleh","transactionlist":[{"ids":["a","b","c","d"]}]}}
我试图将 ID (a,b,c,d) 提取到 Presto 中的行中。从查看其他资源来看,我似乎应该将 "ids" 元素转换为映射,然后转换为数组,最后取消嵌套。但是,我在执行此操作时遇到了一些麻烦,因为 "ids" 元素嵌套在嵌套元素中。有人有任何提示吗?
谢谢!
由于 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)