我在我的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语句必须以这种方式返回数据:
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
类型
unnest()
适用于PostgreSQL的数组类型。使用json_array_elements(json)
(jsonb_array_elements(jsonb)
9.3 + ),(9.4+)或json[b]_array_elements_text(json[b])
(9.4+)