1

我有一个嵌套结构 json 。如何提取特定元素(键)和值?

如何访问“天空:销售”:“1”或“U1”:“0000”?我尝试使用 json_object_keys 和 json_array_elements 来提取数组。但我不确切知道如何进行此查询。示例代码:

表记录和列名:报告

{

  "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"  

                }
         ]

          }                                            }                                                }}  }}

我尝试了很多,这里有一个示例查询,例如,Select * from record r Where exists(select report->'sky: sell' from json_each(r.report) b where b.value->>'sky: sell' Ilike '0 ');

4

1 回答 1

0

您可以使用json_path_queryorjsonb_path_query功能。使用 key = "sky: sell" 提取元素的示例:

json_path_query(r.report, $.** ? (@.key == 'sky:selling'))
于 2021-10-01T12:45:52.637 回答