How do I modify a JSON object?
The post you linked to was mine. That was on an array, but the operator is the same for an object (||) concatentation.


Append a new item like {'d' : 4}

SELECT $${"a":1,"b":2,"c":3}$$::jsonb || '{"d":4}';

Update the key 'c' with the value 5

SELECT ($${"a":1,"b":2,"c":3}$$::jsonb || '{"d":4}') || {"c":5}';

Upsert the already existing key 'd' with the value 6

SELECT (($${"a":1,"b":2,"c":3}$$::jsonb || '{"d":4}') || '{"c":5}') || '{"d":6}';



End result,

{"a": 1, "b": 2, "c": 5, "d": 6}