0

I have a file named data.json. The structure is like this:

 {  "PrimKey1": {    "layout": "normal",    "col1": "PrimKey1",   
 "__colll2__": "sometext",    "col3": 9, "OTHERCOLUMN":"dontneedthis", "col4": ["texxt"] }, 
 ... ,
 {"PrimKey500": {    "layout": "normal",    "col1": "PrimKey500",   
 "col2": "someothertext",    "col3": 1,  "col4": ["texxtagain"] }}

The data is loaded into table a_json with:

CREATE TABLE a_json (
  data json
);

\copy a_json FROM 'mypath/data.json/';

Since the table is not in the expected format, I create a new table named b.

CREATE TABLE b (
  col1      text PRIMARY KEY,
  col2 text,
  col3 numeric,
  col4 text
);

where the columns are named after the columns that I need from data.json.

Now, I want to insert everything from table a_json into b. I tried

INSERT INTO b
SELECT * from a_json json_each(data);

and got

ERROR: index row requires 1945656 bytes, maximum size is 8191

0

1 Answer 1

1

You can just use json_each() and json accessors for this:

insert into b(col1, col2, col3, col4)
select j.v ->> 'col1', j.v ->> 'col2', (j.v ->> 'col3')::numeric, j.v ->> 'col4'
from a_json a
cross join lateral json_each(a.data) j(k, v)

Demo on DB Fiddle:

col1       | col2          | col3 | col4          
:--------- | :------------ | ---: | :-------------
PrimKey1   | sometext      |    9 | ["texxt"]     
PrimKey500 | someothertext |    1 | ["texxtagain"]
Sign up to request clarification or add additional context in comments.

Comments

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.