简体   繁体   English

邮政系统 | 如何从包含多个 JSON 对象的 JSON 数组中提取值

[英]Postgres | How to extract a value from JSON array that contains multiple JSON objects

I have a table that holds a column called additional_info .我有一个表,其中包含一个名为additional_info的列。 this column contains a JSON object that looks like that:此列包含 JSON object,如下所示:

    {
        "dbSources": [{
            "destIp": "10.10.10.29",
            "serviceType": "PostgreSql",
            "srcIp": "10.10.10.68",
            "database": "xe",
            "clusterMember": "",
            "dbId": "PostgreSql_10.10.10.29",
            "clusterName": "",
            "host": "",
            "dbUser": "system",
            "osUser": "",
            "userType": "Unknown",
            "srcApp": ""
        },{
            "destIp": "10.10.10.29",
            "serviceType": "PostgreSql",
            "srcIp": "10.10.10.69",
            "database": "xe1",
            "clusterMember": "",
            "dbId": "PostgreSql_10.10.10.29",
            "clusterName": "",
            "host": "",
            "dbUser": "system",
            "osUser": "",
            "userType": "Unknown",
            "srcApp": ""
        }]
    }

I want to extract (to select) the value of "database" where srcIp equals 10.10.10.68 .我想提取(选择)“数据库”的值,其中srcIp等于10.10.10.68 meaning I want to extract the value "xe" from the first JSON object under the JSON array called dbSources .这意味着我想从名为dbSources的 JSON 数组下的第一个 JSON object 中提取值“xe”。

The only thing that I could do is我唯一能做的就是

    select additional_info::json ->'dbSources' as db from table

but how can I continue from there?但我怎样才能从那里继续呢?

You can do something like that:你可以这样做:

with query as (
select j->>'database' as db,j->>'srcIp' as src_ip from json_array_elements('{
        "dbSources": [{
            "destIp": "10.10.10.29",
            "serviceType": "PostgreSql",
            "srcIp": "10.10.10.68",
            "database": "xe",
            "clusterMember": "",
            "dbId": "PostgreSql_10.10.10.29",
            "clusterName": "",
            "host": "",
            "dbUser": "system",
            "osUser": "",
            "userType": "Unknown",
            "srcApp": ""
        },{
            "destIp": "10.10.10.29",
            "serviceType": "PostgreSql",
            "srcIp": "10.10.10.69",
            "database": "xe1",
            "clusterMember": "",
            "dbId": "PostgreSql_10.10.10.29",
            "clusterName": "",
            "host": "",
            "dbUser": "system",
            "osUser": "",
            "userType": "Unknown",
            "srcApp": ""
        }]
    }'::json->'dbSources') as j)
select db from query where src_ip = '10.10.10.68' 

You can use a JSON path query:您可以使用 JSON 路径查询:

select jsonb_path_query_first(additional_info, '$.dbSources[*] ? (@.srcIp == "10.10.10.68").database')
from the_table    

This assumes the column is of type jsonb (which it should be).这假定该列的类型为jsonb (它应该是)。 If it's not, you need to cast it: additional_info::jsonb如果不是,则需要转换它: additional_info::jsonb

For a table with name table_name, this query will extract the value of database whose srcIp is '10.10.10.68'对于名为 table_name 的表,此查询将提取 srcIp 为 '10.10.10.68' 的数据库的值

SELECT obj::json#>'{database}' as db_name FROM   edisondb.demo r, json_array_elements(r.some_text::json#>'{dbSources}') obj WHERE  obj->>'srcIp' = '10.10.10.68';

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

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