我在以下几行中有一些JSON,不幸的是,它的格式不会改变:
{
"elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
这是存储在postgres数据库中,我想通过上面JSON中嵌入的id来提取记录。到目前为止,我尝试过这样的事情:
SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';
......没有成功;虽然此查询运行但它不返回任何内容任何人都可以建议如何做到这一点?
答案 0 :(得分:1)
创建架构:
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';