小编k4p*_*ppa的帖子

如何在postgres中将json数组转换为行

我在postgres数据库中存储了一个json数组.json看起来像这样:

    [
        {
            "operation": "U",
            "taxCode": "1000",
            "description": "iva description",
            "tax": "12"
        },
        {
            "operation": "U",
            "taxCode": "1001",
            "description": "iva description",
            "tax": "12"
        },
        {
            "operation": "U",
            "taxCode": "1002",
            "description": "iva description",
            "tax": "12"
        }
    ]
Run Code Online (Sandbox Code Playgroud)

现在我需要SELECT数组,以便任何元素都在查询结果的不同行中.所以我执行的SELECT语句必须以这种方式返回数据:

 data
--------------------------------------------------------------------------------------
 { "operation": "U", "taxCode": "1000", "description": "iva description", "tax":"12"}
 { "operation": "U", "taxCode": "1001", "description": "iva description", "tax":"12"}
 { "operation": "U", "taxCode": "1002", "description": "iva description", "tax":"12"}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用这个unnest()功能

SELECT unnest(json_data::json)
FROM my_table
Run Code Online (Sandbox Code Playgroud)

但它不接受这种jsonb类型

arrays postgresql json

45
推荐指数
3
解决办法
4万
查看次数

标签 统计

arrays ×1

json ×1

postgresql ×1