我在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"
}
]现在,我需要对数组执行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"}我尝试使用unnest()函数
SELECT unnest(json_data::json)
FROM my_table但是它不接受jsonb类型。
发布于 2016-05-26 02:34:06
我建议在您的情况下使用json_to_recordset命令。然后,您的SQL应为:
select *
from json_to_recordset('[{"operation":"U","taxCode":1000},{"operation":"U","taxCode":10001}]')
as x("operation" text, "taxCode" int);输出为:
------------------------
| |operation|taxCode |
------------------------
| 1 | "U" | 1000 |
------------------------
| 2 | "U" | 10001 |
------------------------示例中的列(或JSON键)可以自由地进一步扩展。
https://stackoverflow.com/questions/36174881
复制相似问题