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
Syntax
jsonb_insert(target, path, new_value, insert_after)
Parameters
| Parameter | Description |
|---|---|
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
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.
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
| Version | Change |
|---|---|
| PostgreSQL 9.6 | Added |
In other languages
JSON_INSERT()· SQLite
Related
JSONB_SET()· Return jsonb with the value at a path replaced or, by default, created. Combine with || to merge objects.- SQL String Functions · tutorial