小编MrD*_*inB的帖子

在 PostgreSQL 中展平嵌套的 JSON 结构

我正在尝试编写一个 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)

postgresql json nested object

3
推荐指数
1
解决办法
3610
查看次数

标签 统计

json ×1

nested ×1

object ×1

postgresql ×1