在 Postgres 中查询对象数组中缺少的键

问题描述 投票:0回答:1

我正在尝试查询具有对象数组的列内的 json 数据。我正在尝试查找数组中某个对象中缺少键的任何 json

Example: 
    "groups": [{
                "id": "1234575",
                "name": "Lazer Tag team 1",
                "ranking": "1800"
            }, {
                "id": "5634378",
                "name": "Lazer Tag team 2"
                "ranking": 6002
            }, {
                "id": "7568493",
                "name": "Lazer Tag Team 3",
                "ranking": "3215"
            }
        ],

我试图找到的是这些对象中不包含“名称”的任何 Json


Example:


"groups": [{
                "id": "1234575",
                "name": "Lazer Tag team 1",
                "ranking": "1800"
            }, {
                "id": "5634378",
                "ranking": 6002
            }, {
                "id": "7568493",
                "name": "Lazer Tag Team 3",
                "ranking": "3215"
            }
        ],

In this instance this Json is missing "name" in one of the objects so I would like to see a list of all the Jsons that would have this. 

我尝试了以下方法但没有成功:


 select id , JSONB_ARRAY_ELEMENTS(json_message->'payload'->'groups') as partyGroup
 from "notice" n 
 where not (partyGroup ? 'name')

任何帮助将不胜感激。谢谢!

json postgresql
1个回答
0
投票

要查找缺少

name
属性的对象,请使用
jsonb_array_elements
作为横向表函数:

SELECT n.id, partygroup
FROM "notice" n, jsonb_array_elements(n.json_message->'payload'->'groups') AS partygroup
WHERE NOT (partygroup ? 'name');

要查找

name
数组中包含没有
groups
属性的任何对象的行,请在条件的子查询中使用
jsonb_array_elements

SELECT n.id, n.json_message->'payload'->'groups' AS "partyGroups"
FROM "notice" n
WHERE EXISTS (
  SELECT *
  FROM jsonb_array_elements(n.json_message->'payload'->'groups') AS partygroup
  WHERE NOT (partygroup ? 'name')
);
© www.soinside.com 2019 - 2024. All rights reserved.