Sub topic 7
Updating Documents with `jsonb_set`, `||` and `-`
This chapter assumes that you know basic SELECT, UPDATE, WHERE, and PostgreSQL jsonb syntax. It also assumes the orders table from the earlier examples, with a details jsonb column.
A jsonb value is immutable from SQL's point of view. An update function returns a new document, and the UPDATE statement assigns that result back to the column. You can change one path, merge an object, or remove a key without rebuilding the whole document in application code.
Choosing the right update operation
| Operation | Purpose | Scope | Column | Column |
|---|---|---|---|---|
| `jsonb_set` | Replace or create a value at a path | Any path | ||
| ` | ` | Merge two JSON values | Shallow object merge | |
| `-` | Remove a key or array element | Top-level key or array index |
This diagram shows how each operation produces a new document.
Replacing a value with `jsonb_set`
jsonb_set accepts a document, a path, and a replacement value:
jsonb_set(target, path, new_value)The path is a PostgreSQL text array. A path such as '{shipping,city}' means the city key inside the shipping object.
The replacement value must be valid jsonb. A SQL string such as 'shipped' is not the JSON string "shipped" unless you quote it as JSON or convert it with to_jsonb.
UPDATE orders
SET details = jsonb_set(
details,
'{status}',
'"shipped"'::jsonb
)
WHERE id = 1;The expression replaces the existing status value. Because the path has one element, it changes a top-level key.
You can update a nested value in the same way:
UPDATE orders
SET details = jsonb_set(
details,
'{shipping,city}',
'"Stockholm"'::jsonb
)
WHERE id = 1;By default, the fourth argument is true, so PostgreSQL creates the final key when it is missing. Set it to false when you want to replace an existing value only:
UPDATE orders
SET details = jsonb_set(
details,
'{status}',
'"cancelled"'::jsonb,
false
)
WHERE id = 1;If an intermediate object, such as shipping, does not exist, jsonb_set cannot reliably create that entire structure. Build the missing structure first or handle it in application logic.
Merging keys with `||`
The || operator concatenates two jsonb values. For objects, keys from the right-hand value replace keys with the same name on the left.
UPDATE orders
SET details = details || '{"tracking": "X1"}'::jsonb
WHERE id = 1;This adds tracking while preserving the other keys. If tracking already exists, the new value wins:
UPDATE orders
SET details = details || '{"status": "delivered"}'::jsonb
WHERE id = 1;This is a shallow merge. It does not recursively merge nested objects. If both documents contain shipping, the right-hand shipping object replaces the entire left-hand object.
For values other than objects, || has different behavior. Arrays are concatenated, and scalar values are turned into arrays before concatenation. Use it for object patches when you need predictable results.
Removing keys with `-`
The - operator removes a top-level object key:
UPDATE orders
SET details = details - 'tracking'
WHERE id = 1;It can also remove an array element by integer index:
SELECT '["A", "B", "C"]'::jsonb - 1 AS remaining;The result is ["A", "C"]. Array indexes start at zero.
For a nested key, use the #- operator with a path:
UPDATE orders
SET details = details #- '{shipping,city}'
WHERE id = 1;These operators do nothing harmful when the requested key is absent. The original document remains unchanged.
Combining operations safely
You can combine operations in one assignment. The operations are evaluated from the inside out:
UPDATE orders
SET details = jsonb_set(
details || '{"tracking": "X1"}'::jsonb,
'{status}',
'"shipped"'::jsonb
) - 'shipping'
WHERE id = 1;This adds tracking, changes the status, and removes the top-level shipping key. Keep complex updates readable by using a common table expression or separate statements when each transformation needs independent validation.
Always use a selective WHERE clause. Without one, every row receives the transformed document.
Exercise
How would you change order 2's status to "paid" and add a numeric priority of 1?
Show answer
UPDATE orders SET details = jsonb_set(details || '{"priority": 1}'::jsonb, '{status}', '"paid"'::jsonb) WHERE id = 2;