jsonb_insert()

Returns target with new_value inserted. If the item designated by the path is an array element, new_value will be inserted before that item if insert_after is false (which is the default), or after it if insert_after is true. If the item designated by the path is an object field, new_value will be inserted only if the object does not already contain that key. All earlier steps in the path must exist, or the target is returned unchanged. As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. If the last path step is an array index that is out of range, the new value is added at the beginning of the array if the index is negative, or at the end of the array if it is positive.

Signature

jsonb_insert ( target jsonb, path text[], new_value jsonb [, insert_after boolean ] ) → jsonb

Examples

SELECT jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');
{"a": [0, "new_value", 1, 2]}
SELECT jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
{"a": [0, 1, "new_value", 2]}

Run against PostgreSQL 18.6 on 2026-09-13. This is the output we got, not the documentation’s.

Want to run this against your own PostgreSQL server from an iPhone, iPad or Mac? These are ours — every connection goes straight from the device to your database, with no account and no server of ours in between.

Signature and description adapted from the PostgreSQL manual, used under the PostgreSQL License. PostgreSQL is a registered trademark of the PostgreSQL Community Association of Canada. This page is not affiliated with the PostgreSQL Global Development Group.