在列步骤中,我有 json 值,例如 [{"id":"ali","status":"open","reminder":"tomorrow","show_due_date":"true"}]
我想查询新表,例如每个键的单独列
id |状态|提醒|显示到期日期
我编写了这个脚本,但出现了无法在非数组上调用 jsonb_to_recordset 的错误
WITH series (jsonbrecords) AS (Select steps::jsonb from files)
INSERT INTO new
(column1,
column2,
column3,
column4)
SELECT t."id", t."status", t."reminder", t."show_due_date"
FROM series
CROSS JOIN LATERAL
jsonb_array_elements(jsonbrecords) AS x(doc),
jsonb_to_recordset(x.doc) as t("id" text, "status" text,"reminder" text,"show_due_date" text)
最佳答案
您取消了 json 数组的嵌套两次,一次使用 jsonb_array_elements
,一次使用 jsonb_to_recordset
。您只需要其中之一,例如
INSERT INTO new(column1, column2, column3, column4)
SELECT t."id", t."status", t."reminder", t."show_due_date"
FROM files f
CROSS JOIN LATERAL jsonb_to_recordset(f.steps::jsonb) AS t("id" text, "status" text, "reminder" text, "show_due_date" text)
关于sql - 错误: cannot call jsonb_to_recordset on a non-array (postgresql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62406169/