postgres中的嵌套JSON查询
我有以下几行JSON,不幸的是,其格式无法更改:postgres中的嵌套JSON查询,json,postgresql,Json,Postgresql,我有以下几行JSON,不幸的是,其格式无法更改: { "elements": { "nodes": [ { "data": { "name": "Name here", "color": "#FFFFFF", "id": "n0" } } ] } } 这存储在postgres数据库中,我想通过上面JSON中嵌入的id提取记录。到目前为止,我已经尝试过这样
{
"elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
这存储在postgres数据库中,我想通过上面JSON中嵌入的id提取记录。到目前为止,我已经尝试过这样的东西:
SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';
……没有成功;尽管此查询运行,但它不返回任何内容。有人能建议如何做吗?创建模式:
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);
查询本身:
select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node
where node->'data'->>'id' = 'n0';