3

in column steps i have json values like [{"id":"ali","status":"open","reminder":"tomorrow","show_due_date":"true"}]

and i want to query new table like separate column for each key

id | status| reminder | show_due_date

i wrote this script but getting error of cannot call jsonb_to_recordset on a non-array

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)
2
  • MySQL or PostgreSQL? I assume you’re not working with both at the same time with the same query Commented Jun 16, 2020 at 10:24
  • sorry i forget to mention its in the postgresql Commented Jun 16, 2020 at 10:40

2 Answers 2

3

You can bring each respective columns using the below query without need of the other function :

SELECT x.doc ->> 'id' AS id, 
       x.doc ->> 'status' AS status,
       x.doc ->> 'reminder' AS reminder, 
       x.doc ->> 'show_due_date' AS show_due_date
  FROM series
 CROSS JOIN LATERAL jsonb_array_elements(jsonbrecords) AS x(doc)

Demo

You can go on with this query to insert into a new table if you wish :

INSERT INTO new_table(column1,column2,column3,column4)
<the above query>
Sign up to request clarification or add additional context in comments.

7 Comments

thnx for that, its really helpful .but again i have one question if i'm getting my json values from another table. should i have to do the same like that? with series (jsonbrecords) as (Select steps::jsonb from court_files_procedureblueprint) INSERT INTO ahmad VALUES(series.steps::jsonb);
hi @muhammadahmadfalak , you're welcome. Can you add some clarifications, what you exactly want, on the demo which I shared in the answer, please.
actually in the demo you are giving values manually ,but i want that from another table which is files.steps . so i tried to do INSERT INTO series VALUES("files.steps"::jsonb); but not working for me
well, can you share what have you tried in the demo @muhammadahmadfalak ?
dbfiddle.uk/… and here files.steps are with json values
|
2

You're unnesting the json array twice, once with jsonb_array_elements and once with jsonb_to_recordset. You need only one of them, e.g.

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)

9 Comments

thnx for help but it's still giving the same error with this code "ERROR: cannot call jsonb_to_recordset on a non-array"
@muhammadahmadfalak Works fine for me. Can you share your exact table definition and data sample?
yeah its work here, it should have to work for me too. i will look in to that more. thnx for help :) PS : sorry but cannot share exact table
@muhammadahmadfalak Ok, you don't need to share the exact definition, just a complete minimal reproducible example that demonstrates the error if you need further help with it.
acatually files are with six columns and all of the data is getting from django . And steps column is only with the json fields. and yes steps are not uniform means in some rows it have a 2 json values and in some it have 5 or more. but i also research alot and i think i have problem in database section and in retrieving from database it give me error of non-array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.