简体   繁体   English

如何使用 postgresql 访问 json 数组元素(键),json 嵌套字典类型中的值?

[英]How to access json array elements (keys) , values in json nested dictionary type using postgresql?

I have a nested structure json. How to extract the specific elements(keys) and values?我有一个嵌套结构 json。如何提取特定元素(键)和值?

How to access " sky: selling":"1" Or "U1":"0000"?如何访问“天空:销售”:“1”或“U1”:“0000”? I tried json_object_keys and json_array_elements for extracting the array.我尝试使用 json_object_keys 和 json_array_elements 来提取数组。 But I don't know exactly how to do this query.但我不知道如何做这个查询。 Example code:示例代码:

Table- record and column name: report表记录和列名:report

{

  "PIname": {

    "n1": "x1",

                "n2": "x2",

                "params": {

                "S1": {

        "code1": "y1",

        "Code2": "y2",

      },

      "id": "2d",

      "Dest": {

        "Code3": "mi"

      }

    },

                "PIDataArea": {

                "m1": null,

      "PInven": {

        "head": {

          "Code4": "Increase",

          "line": "2020-01"

        },

                                "PILine": [

          {

            "u1": "0000",

            "u2": "0",

            "u3": "1",

            "modes": {

              "@cID": "Sng",

              "#txt": "12.21"

            }                                               },
                                               {
            "Qualify": ".0001",

            "QOrder": "1",

            "UPriceAmt": {

              "@cID": "sng",

              "#txt": "13"                                              },

            "sky:Qa": ".000",

            "sky:Partcode": {

              "@c1ID": "a"   

            },

            "sky:SCode": "Ni",

            "sky:PItem": {

              "sky:ID": "h"   

            },

            "sky:Forest": {

              "sky:q1": [

                {

                  "sky:selling": "1"
                }

                {

                  "sky:selling": "0"  

                }
         ]

          }                                            }                                                }}  }}

I tried lot,one example query here like, Select * from record r Where exists( select report->'sky: selling' from json_each(r.report) b where b.value->>'sky:selling' Ilike '0');我尝试了很多,这里有一个示例查询,例如 Select * 来自记录 r 哪里存在(select report->'sky: selling' from json_each(r.report) b where b.value->>'sky:selling' Ilike '0 ');

You can use the json_path_query or jsonb_path_query function. Example to extract the element with key = "sky:selling":您可以使用json_path_queryjsonb_path_query function。使用 key = "sky:selling" 提取元素的示例:

json_path_query(r.report, $.** ? (@.key == 'sky:selling'))

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

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