python - 如何在postgres中将两个相似的JSON树合并成一个带有数组的大胖树?
问题描述
所以我在 Postgres DB 中有两个 JSON 树:
item_id | jsonb_pretty
---------+---------------------------------------
1 | { +
| "title": "PARENT", +
| "item_id": 1, +
| "children": { +
| "title": "LEVEL 2", +
| "item_id": 2, +
| "children": { +
| "title": "LEVEL 3.2", +
| "item_id": 6 +
| } +
| } +
| }
2 | { +
| "title": "PARENT", +
| "item_id": 1, +
| "children": { +
| "title": "LEVEL 2", +
| "item_id": 2, +
| "children": { +
| "title": "LEVEL 3.1", +
| "item_id": 3, +
| "children": { +
| "title": "LEVEL 4.1",+
| "item_id": 4 +
| } +
| } +
| } +
| }
我想在 Postgres 中将这些树合并为一棵,以便具有相同键的元素变成多个元素的数组。例子:
item_id | jsonb_pretty
3 {
"title": "PARENT",
"item_id": 1,
"children": [{
"title": "LEVEL 2",
"item_id": 2,
"children": [{
"title": "LEVEL 3.2",
"item_id": 6
},
{
"title": "LEVEL 3.1",
"item_id": 3,
"children": {
"title": "LEVEL 4.1",
"item_id": 4
}
}]
}]
}
如何在 Postgres 13 中做这样的事情?
解决方案
推荐阅读
- php - 如何添加多个订单项
使用 qbxml 和 php 动态添加销售收据? - javascript - NodeJS Rest API 发送一个空数组
- ember.js - 在 ember 中为现有路由添加父路由
- java - A warning is shown when Optional.isPresent and Optional.get are called "on different" levels?
- angular - 组件不是已知元素 [Angular]
- java - OpenJDK开发中如何使用STL函数?
- django - django 验证用户并生成 JWT 令牌
- python - 在变量中获取 stderr 并使用可能需要从 stdin 读取的子进程实时打印它
- segmentation-fault - QLabel.setPixmap 与 PyQt5 后的分段错误
- django - Django-filter - 过滤带注释的字段