sql - 错误: cannot call jsonb_to_recordset on a non-array (postgresql)

标签 sql postgresql jsonb

在列步骤中,我有 json 值,例如 [{"id":"ali","status":"open","re​​minder":"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/

相关文章:

arrays - JSON 数组列转换为 JSON 对象

sql - MS Access/SQL 问题 : How to split strings?

c# - 使用 SqlCommand.ExecuteNonQuery,如何获得超过 int 限制的受影响行数?

SQL查询错误插入到

postgresql - 在文件系统上移动文件的 PostgreSQL 触发器

postgresql - 如何将 postgresql 9.4 jsonb 转换为没有函数/服务器端语言的对象

mysql - 修改子查询中的日期会减慢执行速度

mysql - 仅当项目按升序排列时,如何选择(SQL)项目?

python - PostgreSQL Sqlalchemy 提交需要很多时间

sql - 如何在 Postgresql 中对 JSONB 数组中的值求和?