Add element to JSON object in Postgres

If you upgrade to PG9.5.1, then you can use sql operator || to merge jsonb, example

select '{"a":1}'::jsonb || '{"a":2, "b":2}'::jsonb

will return {"a": 2, "b": 2}

If you can’t upgrade to pg9.5.1, IMHO, doing the job in your code will be a better choice. You can parse old jsonb string as a map, and then update the map, then convert to string and update db-record.

And if we want to update (add) a JSONB field:

UPDATE <table>
SET <field-name> = <field-name> || '{"a": 1}'::jsonb
WHERE id = <some id>

Documentation detailing all JSON operations in Postgres 9.5

Leave a Comment