1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-31 22:04:40 +03:00

Rename jsonb_replace to jsonb_set and allow it to add new values

The function is given a fourth parameter, which defaults to true. When
this parameter is true, if the last element of the path is missing
in the original json, jsonb_set creates it in the result and assigns it
the new value. If it is false then the function does nothing unless all
elements of the path are present, including the last.

Based on some original code from Dmitry Dolgov, heavily modified by me.

Catalog version bumped.
This commit is contained in:
Andrew Dunstan
2015-05-31 20:34:10 -04:00
parent 75f9d17638
commit 37def42245
9 changed files with 384 additions and 126 deletions

View File

@ -10323,6 +10323,15 @@ table2-mapping
</tgroup>
</table>
<note>
<para>
The <literal>||</> operator concatenates the elements at the top level of
each of its operands. It does not operate recursively. For example, if
both operands are objects with a common key field name, the value of the
field in the result will just be the value from the right hand operand.
</para>
</note>
<para>
<xref linkend="functions-json-creation-table"> shows the functions that are
available for creating <type>json</type> and <type>jsonb</type> values.
@ -10830,17 +10839,24 @@ table2-mapping
<entry><literal>[{"f1":1},2,null,3]</literal></entry>
</row>
<row>
<entry><para><literal>jsonb_replace(target jsonb, path text[], replacement jsonb)</literal>
<entry><para><literal>jsonb_set(target jsonb, path text[], new_value jsonb<optional>, <parameter>create_missing</parameter> <type>boolean</type></optional>)</literal>
</para></entry>
<entry><para><type>jsonb</type></para></entry>
<entry>
Returns <replaceable>target</replaceable>
with the section designated by <replaceable>path</replaceable>
replaced by <replaceable>replacement</replaceable>.
with the section designated by <replaceable>path</replaceable>
replaced by <replaceable>new_value</replaceable>, or with
<replaceable>new_value</replaceable> added if
<replaceable>create_missing</replaceable> is true ( default is
<literal>true</>) and the item
designated by <replaceable>path</replaceable> does not exist.
</entry>
<entry><literal>jsonb_replace('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]')</literal></entry>
<entry><literal>[{"f1":[2,3,4],"f2":null},2,null,3]</literal>
</entry>
<entry><para><literal>jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)</literal>
</para><para><literal>jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')</literal>
</para></entry>
<entry><para><literal>[{"f1":[2,3,4],"f2":null},2,null,3]</literal>
</para><para><literal>[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]</literal>
</para></entry>
</row>
<row>
<entry><para><literal>jsonb_pretty(from_json jsonb)</literal>
@ -10891,6 +10907,27 @@ table2-mapping
</para>
</note>
<note>
<para>
All the items of the <literal>path</> parameter of <literal>jsonb_set</>
must be present in the <literal>target</>, unless
<literal>create_missing</> is true, in which case all but the last item
must be present. If these conditions are not met the <literal>target</>
is returned unchanged.
</para>
<para>
If the last path item is an object key, it will be created if it
is absent and given the new value. If the last path item is an array
index, if it is positive the item to set is found by counting from
the left, and if negative by counting from the right - <literal>-1</>
designates the rightmost element, and so on.
If the item is out of the range -array_length .. array_length -1,
and create_missing is true, the new value is added at the beginning
of the array if the item is negative, and at the end of the array if
it is positive.
</para>
</note>
<note>
<para>
The <literal>json_typeof</> function's <literal>null</> return value