操作Postgres中的嵌套JSON
我对postgres比较陌生,操作嵌套JSON数组时遇到问题 我的JSON结构:操作Postgres中的嵌套JSON,json,postgresql,Json,Postgresql,我对postgres比较陌生,操作嵌套JSON数组时遇到问题 我的JSON结构: { "rows":[ { "columns":[ [ { "text":"Some text", "type":"report" }, { "type":"feedback" } ], [
{
"rows":[
{
"columns":[
[
{
"text":"Some text",
"type":"report"
},
{
"type":"feedback"
}
],
[
{
"key":"p0",
"type":"publications",
"title":""
}
]
]
},
{
"columns":[
[
{
"key":"p1",
"type":"publication",
"title":""
}
]
]
}
]
}
如您所见,json结构有点冗余,这使得它更加困难。我的目标只是在type=“report”
的每个嵌套列中添加一个新的title元素(正如type=“publication”
中已经存在的那样)
我目前正在数组上循环,并试图通过concateration重新构建修改后的json结构,但对于这个简单的任务来说,这是一个乏味而烦人的过程
FOR currentColumn IN SELECT * FROM json_array_elements(extractedRows)
-- more for loops to get to the type="publication" elements
-- set the new title element
SELECT jsonb_set(...) into result;
-- finally reconstruct the json and persist it
END LOOP;
有没有聪明的方法可以更有效地做到这一点?我已经阅读了文档,但是我很难想出更好的方法