PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_INSERT() Function

Insert a value into a JSON array at a path (before or after an element) or add a new object key.

PostgreSQL 18 Since 9.6

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

Syntax

jsonb_insert(target, path, new_value, insert_after)

Parameters

ParameterDescription
target The original jsonb.
path Where to insert.
new_value The value.
insert_after optional Insert after the element instead of before. Default: false

Return value

jsonb.

Examples

Insert into an array
SELECT jsonb_insert('{"tags":["a","c"]}', '{tags,1}', '"b"') AS inserted;

Output

         inserted
---------------------------
 {"tags": ["a", "b", "c"]}
(1 row)

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

Append at the end
SELECT jsonb_insert('[1,2]', '{-1}', '3', true) AS appended;

Output

 appended
-----------
 [1, 2, 3]
(1 row)

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

Version history

VersionChange
PostgreSQL 9.6Added

In other languages

Was this page helpful?