简体   繁体   English

使用 Presto 从列中提取 JSON 数据

[英]Extract JSON data from column using Presto

I am using Presto and have a column_A with complex JSON data where each cell has an array that looks like this:我正在使用 Presto,并且有一个带有复杂 JSON 数据的 column_A,其中每个单元格都有一个如下所示的数组:

[{"value":"aaa", "items":["246","123"],...}, {"value":"bbb", "items":["357","123"],...}]

What I am trying to do is extract from each cell only the JSON object where "items" has code "357".我要做的是从每个单元格中仅提取“项目”具有代码“357”的 JSON 对象。

So ideally the output of the query would be new data in Column_B that has one JSON object (not an array) and looks like this:因此,理想情况下,查询的输出将是 Column_B 中具有一个JSON 对象(不是数组)的新数据,如下所示:

{"value":"bbb", "items":["357","123"],...}

Note: for each cell, there is only one object with the code "357", however, "item" may have more than 1 code inside its array.注意:对于每个单元格,只有一个代码为“357”的对象,但是,“项目”的数组中可能有多个代码。 As in the examples above.就像上面的例子一样。

So far I tried the below, but it doesnt seem to be working:到目前为止,我尝试了以下方法,但它似乎不起作用:

SELECT JSON_EXTRACT(column_a, '$.(SELECT * from column_a where column_a::text LIKE '%357%')') AS column_extracted
FROM column_a

Does anyone have an idea of what I could try instead?有谁知道我可以尝试什么?

You can use the newly introduced JSON path functions in Trino (formerly known as Presto SQL) for this:您可以为此使用 Trino(以前称为 Presto SQL)中新引入的JSON 路径函数:

WITH t(data) AS (
    VALUES '[{"value":"aaa", "items":["246","123"]}, {"value":"bbb", "items":["357","123"]}]'
)

SELECT json_query(t.data, 'strict $[*]?(@.items[*] == "357")')
FROM t

which produces:产生:

                 _col0
---------------------------------------
 {"value":"bbb","items":["357","123"]}
(1 row)

If you look at $[*]?(@.items[*] == "357") , it says "find all the elements of the top-level array that match the condition @.items[*] == "357" ", which, in turn, checks whether any of the elements in the items array has a value of 357 .如果您查看$[*]?(@.items[*] == "357") ,它会说“查找与条件@.items[*] == "357"匹配的顶级数组的所有元素@.items[*] == "357" ",它依次检查items数组中的任何元素是否具有357的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM