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:
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:
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
)
p_attributes
(plural)