PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_SET() Function

Return jsonb with the value at a path replaced or, by default, created. Combine with || to merge objects.

PostgreSQL 18 Since 9.5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

jsonb_set(target, path, new_value, create_if_missing)

Parameters

ParameterDescription
target The original jsonb.
path A text array path, such as '{address,city}'.
new_value The new jsonb value.
create_if_missing optional Add the key if it doesn't exist. Default: true

Return value

jsonb.

Examples

Change a nested value
SELECT jsonb_set('{"name":"Ada","address":{"city":"London"}}', '{address,city}', '"Paris"') AS moved;

Output

                     moved
-----------------------------------------------
 {"name": "Ada", "address": {"city": "Paris"}}
(1 row)

Run this on your computer — see “Get Started” for setup.

Merge with || and remove with -
SELECT '{"theme":"light"}'::jsonb || '{"lang":"en"}' AS merged,
       '{"a":1,"b":2}'::jsonb - 'b' AS removed;

Output

              merged              | removed
----------------------------------+----------
 {"lang": "en", "theme": "light"} | {"a": 1}
(1 row)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.5Added

In other languages

Was this page helpful?