PostgreSQL 9.5: How to Concatenate and Overwrite JSON Document

The PostgreSQL JSON data type is also one of the my favorite topic.
I have written multiple article on PostgreSQL JSON data type.

PostgreSQL 9.4: The JSON data type is Awesome (Part 1/3)

PostgreSQL 9.5 introduced one new operator (||) which used to perform Concatenate and Overwrite of JSON Document.

The requirement is something like: we have stored one JSON string or document into PostgreSQL. Now we require to add any new key or elements into that document.

It was not possible in the previous version of PostgreSQL, but now we can use (||) this operator to add or overwrite particular key into the JSON document.

Please go through this full practical demonstration:

Create a table with Sample data:

Using (||) operator can concat two JSON String:
In the below example, you can see “address” key is merged with “employees” key.

Using (||) operator can overwrite the JSON string:
In the below example, you can see the replacement of “address” value.

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of