【发布时间】:2021-02-08 23:41:45
【问题描述】:
我正在运行 SQL 查询以提取嵌套的 JSON 数据。
SELECT watsonResponse.responseId,
watsonResponse.chatId,
d.*
FROM watson_response_table watsonResponse
CROSS JOIN LATERAL (
SELECT d2.*
FROM jsonb_array_elements(watsonResponse.watsonresponse_output) AS d(events)
CROSS JOIN LATERAL (
SELECT d2.events ->> 'data' AS watsonResponse_ouput_data
, d2.events ->> 'text' AS watsonResponse_output_text
, d2.events ->> 'uiActionCode' AS watsonResponse_output_uiActionCode
FROM jsonb_array_elements(d.events) AS d2(events)
) d2
WHERE d.events ->> 'uiActionCode' = 'TextWithButton'
) d;
失败并显示消息 SQL 错误 [22023]:错误:无法从对象中提取元素
我正在使用 PostgresSQL 11+。这是 JSON 的样子,
[
{
"text": [
"Some text!"
],
"uiActionCode": "textOnly"
},
{
"data": {
"type": "options",
"options": [
{ "label": "test", "value": "testvalue" },
{ "label": "test2", "value": "testvalue2" },
{
"label": "test3",
"value": "testQuestion?"
}
]
},
"text": ["testQuestion2?"],
"uiActionCode": "TextWithButton"
}
]
【问题讨论】:
-
请向我们展示您想要的样本数据结果。
-
我想知道为什么我的 SQL 查询失败了。 sql 查询应该成功返回我的 SQL 编辑器中的数据。它将有五列(responseId、chatId、data、text、uiActionCode)。具体来说,数据列应该包含数据中的所有内容:{}; text 应该有“testQuestion2”作为其列中的值,而 uiActionCode 将有 TextWithButton。
标签: sql arrays json postgresql unnest