简体   繁体   English

Postgres中的嵌套JSON查询

[英]Nested JSON query in postgres

I have some JSON along the following lines, the format of which cannot, unfortunately, be changed: 我在以下几行中有一些JSON,不幸的是,其格式无法更改:

{ 
  "elements": {
    "nodes": [
      {
        "data": {
          "name": "Name here",
          "color": "#FFFFFF",
          "id": "n0"
        }
      }
    ]
  }
}

This is stored in a postgres database and I'd like to pull out records by means of the id embedded in the JSON above. 这存储在一个postgres数据库中,我想通过上面JSON中嵌入的id提取记录。 So far I've tried stuff like this: 到目前为止,我已经尝试过这样的事情:

SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';

...without success; ...没有成功; although this query runs it returns nothing. 尽管此查询运行,但不返回任何内容。 Can anyone suggest how this might be done? 谁能建议这怎么做?

Create schema: 创建架构:

create table json (
    id serial primary key,
    data jsonb
);

insert into json (data) values (
'{ "elements": {
    "nodes": [
      {
        "data": {
          "name": "Name here",
          "color": "#FFFFFF",
          "id": "n0"
        }
      }
    ]
  }
}
'::jsonb);

Query itself: 查询本身:

select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node 
 where node->'data'->>'id' = 'n0';

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

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