How to insert a created_date and updated_date in J

2019-08-27 03:08发布

问题:

{"request": 12, "createdby": "sam"} is the JSON data to be inserted in a PostgreSQL table with two columns request id int, data JSONB.

The Jain data can be inserted into data column with JSONB datatype. can we append created_date in the JSON object before inserting it into the data column.

回答1:

sure, you can just concat jsonb with || operator

t=# create table so9(rid int, d jsonb);
CREATE TABLE
t=# insert into so9 (d) select '{"request": 12, "createdby": "sam"}'::jsonb || concat('{"created_date":"',now(),'"}')::jsonb;
INSERT 0 1
t=# select * from so9;
 rid |                                          d
-----+--------------------------------------------------------------------------------------
     | {"request": 12, "createdby": "sam", "created_date": "2018-03-30 17:24:05.246852+01"}
(1 row)