我有一个JSON列,如下所示。我正在尝试提取ID和标识符列的值(即identifier1,identifier2,identifier3和identifier4),但我无法获取数据。下面给出了我试图从中提取的JSON数据的示例。
[{"id": 5079, "identifier1": {"key": "ABC", "type": "identifier", "regex": null},
"identifier2": {"key": 123, "type": "identifier", "regex": null},
"identifier3": {"key": XYZ, "type": "identifier", "regex": null},
"identifier4": {"key": ABC, "type": "identifier", "regex": null}}]
列的数据类型是JSONB DB:Postgres ColumnName:jsondata
我尝试了以下查询,但返回了空输出
select
jsondata ->> 'id',
jsondata -> 'identifier1' ->> 'key'
from Customer;
任何人都可以帮我解决这个问题。感谢
答案 0 :(得分:0)
我不确定我的问题是否正确,但我想你正在寻找的是这样的:
SELECT
json_array_elements(jsondata)->>'id' AS id,
(json_array_elements(jsondata)->>'identifier1')::json ->> 'key' AS key1,
(json_array_elements(jsondata)->>'identifier2')::json ->> 'key' AS key2,
(json_array_elements(jsondata)->>'identifier3')::json ->> 'key' AS key3,
(json_array_elements(jsondata)->>'identifier4')::json ->> 'key' AS key4
from Customer;