[英]Presto extract string from array of JSON elements
I am on Presto 0.273 and I have a complex JSON data from which I am trying to extract only specific values.我在 Presto 0.273 上,我有一个复杂的 JSON 数据,我试图从中提取特定的值。
First, I ran SELECT JSON_EXTRACT(library_data, '.$books')
which gets me all the books from a certain library.首先,我运行了
SELECT JSON_EXTRACT(library_data, '.$books')
,它从某个图书馆获取了所有书籍。 The problem is this returns an array of JSON objects that look like this:问题是这会返回一个 JSON 对象数组,如下所示:
[{
"book_name":"abc",
"book_size":"453",
"requestor":"27657899462"
"comments":"this is a comment"
}, {
"book_name":"def",
"book_size":"354",
"requestor":"67657496274"
"comments":"this is a comment"
}, ...
]
I would like the code to return just a list of the JSON objects, not an array.我希望代码只返回 JSON 对象的列表,而不是数组。 My intention is to later be able to loop through the JSON objects to find ones from a specific requester.
我的意图是稍后能够遍历 JSON 对象以从特定请求者那里找到对象。 Currently, when I loop through the given arrays using python, I get a range of errors around this data being a Series, hence trying to extract it properly rather.
目前,当我使用 python 循环遍历给定的数组时,我得到了围绕这个数据的一系列错误是一个系列,因此试图正确地提取它。
I tried this SELECT JSON_EXTRACT(JSON_EXTRACT(data, '$.domains'), '$[0]')
but this doesn't work because the index position of the object needed is not known.我试过这个
SELECT JSON_EXTRACT(JSON_EXTRACT(data, '$.domains'), '$[0]')
但这不起作用,因为所需对象的索引位置未知。
I also tried SELECT array_join(array[books], ', ')
but getting "Error casting array element to VARCHAR " error.我也尝试过
SELECT array_join(array[books], ', ')
但出现“将数组元素转换为 VARCHAR 时出错”错误。
Can anyone please point me in the right direction?谁能指出我正确的方向?
Cast to array(json)
:转换为
array(json)
:
SELECT CAST(JSON_EXTRACT(library_data, '.$books') as array(json))
Or you can use it in unnest
to flatten it to rows:或者您可以在
unnest
中使用它来将其展平为行:
SELECT *,
js_obj -- will contain single json object
FROM table
CROSS JOIN UNNEST CAST(JSON_EXTRACT(library_data, '.$books') as array(json)) as t(js_obj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.