I have table_A with two columns:
create table table_a(id int, data json);
A row could be:
insert into table_a values
(1, '{"name": "kate", "cellphone": "000-000-0000", "address": "some text here"}');
I want to write a function that will take a row from table_A and insert a new row into table_B. Table_B has columns: id integer, name VARCHAR, cellphone VARCHAR, address TEXT, additional_info TEXT.
So my function should parse json field and put each value in the corresponding column of the Table_B (assume that all possible columns exist in Table_B).
It looks like I can use json_to_record(json), but how can I insert returned values into Table_B?
I am using PyGreSQL to connect with my database.
You should use the function in a lateral join. A column definition list should be added as the function returns
record
.The insert statement may look like this: