JSONB-modifying operators and functions
In 9.3 (and to a greater extent in 9.4), JSONB data could be extracted using various functions and operators, but nothing that could actually modify the data. As of 9.5, JSONB data can now be modified.
jsonb || jsonb (concatenate / overwrite)
The || operator allows us to combine 2 jsonb objects. If there‘s overlap, values are replaced on the highest level.
For example, if we want to add values to a jsonb object:
# SELECT ‘{"name": "Joe", "age": 30}‘::jsonb || ‘{"town": "London"}‘::jsonb;
?column?
----------------------------------------------
{"age": 30, "name": "Joe", "town": "London"}
(1 row)
Or we can overwrite existing values:
# SELECT ‘{"town": "Dataville", "population": 4096}‘::jsonb || ‘{"population": 8192}‘::jsonb;
?column?
-------------------------------------------
{"town": "Dataville", "population": 8192}
(1 row)
Note that this only works on the highest level, so nested objects are replaced from the top level. For example:
# SELECT ‘{"name": "Jane", "contact": {"phone": "01234 567890", "mobile": "07890 123456"}}‘::jsonb || ‘{"contact": {"fax": "01987 654321"}}‘::jsonb;
?column?
------------------------------------------------------
{"name": "Jane", "contact": {"fax": "01987 654321"}}
(1 row)
jsonb - text / int (remove key / array element)
We can remove keys from a jsonb object with the - operator:
# SELECT ‘{"name": "James", "email": "[email protected]"}‘::jsonb - ‘email‘;
?column?
-------------------
{"name": "James"}
(1 row)
Or remove values from an array (base 0):
# SELECT ‘["red","green","blue"]‘::jsonb - 1;
?column?
-----------------
["red", "blue"]
(1 row)
jsonb - text[] / int (remove key / array element in path)
The previous example, we can remove keys or array elements, but not any lower than the highest level, so we can provide a path to the value we want to delete using a text array. Here we‘ll want to remove the fax number from within the contact value:
# SELECT ‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb - ‘{contact,fax}‘::text[];
?column?
---------------------------------------------------------
{"name": "James", "contact": {"phone": "01234 567890"}}
(1 row)
Or we can remove an array value. Here we‘ll get rid of the array value as index 1 (2nd value):
# SELECT ‘{"name": "James", "aliases": ["Jamie","The Jamester","J Man"]}‘::jsonb - ‘{aliases,1}‘::text[];
?column?
--------------------------------------------------
{"name": "James", "aliases": ["Jamie", "J Man"]}
(1 row)
jsonb_replace function
The above lets us delete values in a path, but not update them, so we have the jsonb_replace function for that. We‘ll update the phone value within the contact value:
# SELECT jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb);
jsonb_replace
--------------------------------------------------------------------------------
{"name": "James", "contact": {"fax": "01987 543210", "phone": "07900 112233"}}
(1 row)
jsonb_pretty
Notice that jsonb doesn‘t preserve white-space, so no matter how effort you went to in order to make the object easier to read, it will end up as a long string. Well jsonb_pretty will format it for you. If we use the previous jsonb example and wrap it all in a jsonb_pretty function:
# SELECT jsonb_pretty(jsonb_replace(‘{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}‘::jsonb, ‘{contact,phone}‘, ‘"07900 112233"‘::jsonb));
jsonb_pretty
---------------------------------
{ +
"name": "James", +
"contact": { +
"fax": "01987 543210", +
"phone": "07900 112233"+
} +
}
(1 row)
Much easier to read.