0

I want to insert data into a table where the first level keys are inserted as values and not as json. Although column "p_attribute" needs to store the nested json. For example, in case of Jane Doe, I want her attribute column to look like this:

Desired query result

I can only achieve this by updating the column p_attribute with:

update json_table
set p_attribute =(
    '{
    "p_attribute": {
    "age": "37",
    "eye_color": "blue",
    "favourite_qoute": "I am the classic example"
    }
    }'
)

Now I want to create the above picture in one insert, like this:

insert into json_table
select * from json_populate_recordset (NULL::json_table,
    '[{
    "p_id": 1,
    "first_name":   "Jane",
    "last_name":    "Doe",
    "p_attributes": {
        "age": "37",
        "eye_color": "blue",
        "favourite_qoute": "I am the classic example"
        }
    }]';

But I execute this insert then the p_attribute column gets a null but the rest of the columns are correctly inserted:

Json column is null

Im using PostgreSQL 12 and my table looks like this:

create table json_table (
    p_id int primary key,
    first_name varchar(20),
    last_name varchar(20),
    p_attribute json
)  
1
  • You will need to name your column p_attributes (plural)
    – user1822
    Commented May 26, 2020 at 13:42

1 Answer 1

0

The actual column name "p_attribute" and the key "p_attributes" in the json format were different...

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.