Postgres jsonb,嵌套数组查询隐藏特定字段

时间:2017-03-15 08:19:15

标签: postgresql postgresql-9.4 jsonb

我有一个以下格式的jsonb数据,带有嵌套数组

{
  "outerArray": [
    {
      "price": {
        "amount": 108.95,
        "currencyCode": "GBP"
      },
      "innerArray": [
        {
          "details": {
            "field1": "val1",
            "field2": "val2",
            "field3": "val3"
          },
          "otherDetail": {
            "date": "2016-07-23",
            "time": "19:43:00"
          },
          "innerMostArray": [
            {
              "A1": "A1"
            },
            {
              "B1": "B1"
            }
          ]
        }
      ],
      "someField": "values"
    },
    {
      "price": {
        "amount": 108.95,
        "currencyCode": "GBP"
      },
      "innerArray": [
        {
          "details": {
            "field1": "val1",
            "field2": "val2",
            "field3": "val3"
          },
          "otherDetail": {
            "date": "2016-07-23",
            "time": "19:43:00"
          },
          "innerMostArray": [
            {
              "A1": "A1"
            },
            {
              "B1": "B1"
            }
          ]
        }
      ],
      "someField": "values"
    }
  ]
}

我想在此上写一个检索查询,以保持相同的json结构,但隐藏字段" price"," details" ," otherDetail"和" someField"

检索到的结果应如下所示

{
  "outerArray": [
    {
      "innerArray": [
        {
          "innerMostArray": [
            {
              "A1": "A1"
            },
            {
              "B1": "B1"
            }
          ]
        }
      ]
    },
    {
      "innerArray": [
        {
          "innerMostArray": [
            {
              "A1": "A1"
            },
            {
              "B1": "B1"
            }
          ]
        }
      ]
    }
  ]
}

可以这样做吗?

1 个答案:

答案 0 :(得分:1)

请始终指定您正在使用的PostgreSQL版本。下面的示例应该适用于版本v9.5 +。

我会通过使用jsonb_build_object()jsonb_build_array()函数构建您需要的JSONB对象来实现此目的:

示例查询:

WITH test(data) AS ( VALUES
  ('{
      "outerArray": [
        {
          "price": {
            "amount": 108.95,
            "currencyCode": "GBP"
          },
          "innerArray": [
            {
              "details": {
                "field1": "val1",
                "field2": "val2",
                "field3": "val3"
              },
              "otherDetail": {
                "date": "2016-07-23",
                "time": "19:43:00"
              },
              "innerMostArray": [
                {
                  "A1": "A1"
                },
                {
                  "B1": "B1"
                }
              ]
            }
        ],
        "someField": "values"
      },
    {
      "price": {
        "amount": 108.95,
        "currencyCode": "GBP"
      },
      "innerArray": [
        {
          "details": {
            "field1": "val1",
            "field2": "val2",
            "field3": "val3"
          },
          "otherDetail": {
            "date": "2016-07-23",
            "time": "19:43:00"
          },
          "innerMostArray": [
            {
              "A1": "A1"
            },
            {
              "B1": "B1"
            }
          ]
        }
      ],
      "someField": "values"
    }
  ]}'::JSONB)
)
SELECT
  jsonb_build_object(
    'outerArray',
    array_agg(
        jsonb_build_object(
            'innerArray',
            json_build_array(
                json_build_object(
                    'innerMostArray',
                    innerArray->'innerMostArray')
            )
        )
    )
  ) as result
FROM test t,
    jsonb_array_elements(t.data->'outerArray') as outerElement,
    jsonb_array_elements(outerElement->'innerArray') as innerArray;

结果:

    result                                                                          
----------------------------------------------------------------------------------------------------------------------------------------------------------
 {"outerArray": [{"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}, {"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}]}
(1 row)