我正在尝试编写一个 Postgres 查询,它将json以特定格式输出我的数据。
JSON 数据结构
{
user_id: 123,
data: {
skills: {
"skill_1": {
"title": "skill_1",
"rating": 4,
"description": 'description text'
},
"skill_2": {
"title": "skill_2",
"rating": 2,
"description": 'description text'
},
"skill_3": {
"title": "skill_3",
"rating": 5,
"description": 'description text'
},
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我最终需要格式化数据的方式:
[
{
user_id: 123,
skill_1: 4,
skill_2: 2,
skill_3: 5,
...
},
{
user_id: 456,
skill_1: 1,
skill_2: 3,
skill_3: 4,
...
}
]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我正在使用如下所示的查询:
SELECT
user_id,
data#>>'{skills, "skill_1", rating}' AS …Run Code Online (Sandbox Code Playgroud)