简体   繁体   English

在 PostgreSQL 查询中使用 json_extract_path_text 和 json_array_elements 解析 JSONarray

[英]Parse JSONarray in PostgreSQL Query using json_extract_path_text and json_array_elements

I have a PostgreSQL query like the following that works and returns foo as expected.我有一个 PostgreSQL 查询,如下所示,它可以正常工作并按预期返回 foo。

SELECT json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6') as foo

This does not work.这是行不通的。 It just returns null.它只返回 null。

SELECT json_extract_path_text('{"f2":{"f3":1},"f4":[{"f5":99,"f6":"foo"},{"f5":99,"f6":"foo"}]}','f4', 'f6') as foo

I have tried about every combination of the functions I can find at PostgreSQL Function website.我已经尝试了在PostgreSQL Function网站上可以找到的所有功能组合。 Thus my question is, how do I get to the foo in the json array?因此我的问题是,如何到达 json 数组中的 foo?

so I did get this to work, but it seems like there has to be a better way:所以我确实让它起作用了,但似乎必须有更好的方法:

SELECT 
json_extract_path_text(
    json_array_elements(
        cast(json_extract_path_text('{"f2":{"f3":1},"f4":[{"f5":99,"f6":"foo"},{"f5":99,"f6":"foo"}]}'::json,'f4') as json
            )
    ),'f6'
) as foo

Found another way to accomplish something similar that may be useful找到了另一种方法来完成可能有用的类似事情

someJsonColumn::json #>> '{someObj,0,SomeObj,0}' as myJsonField

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

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