1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Additional functions and operators for jsonb

jsonb_pretty(jsonb) produces nicely indented json output.
jsonb || jsonb concatenates two jsonb values.
jsonb - text removes a key and its associated value from the json
jsonb - int removes the designated array element
jsonb - text[] removes a key and associated value or array element at
the designated path
jsonb_replace(jsonb,text[],jsonb) replaces the array element designated
by the path or the value associated with the key designated by the path
with the given value.

Original work by Dmitry Dolgov, adapted and reworked for PostgreSQL core
by Andrew Dunstan, reviewed and tidied up by Petr Jelinek.
This commit is contained in:
Andrew Dunstan
2015-05-12 15:52:45 -04:00
parent afb9249d06
commit c6947010ce
9 changed files with 1813 additions and 16 deletions

View File

@ -10293,6 +10293,32 @@ table2-mapping
<entry>Do all of these key/element <emphasis>strings</emphasis> exist?</entry>
<entry><literal>'["a", "b"]'::jsonb ?&amp; array['a', 'b']</literal></entry>
</row>
<row>
<entry><literal>||</literal></entry>
<entry><type>jsonb</type></entry>
<entry>Concatentate two jsonb values into a new jsonb value</entry>
<entry><literal>'["a", "b"]'::jsonb || '["c", "d"]'::jsonb</literal></entry>
</row>
<row>
<entry><literal>-</literal></entry>
<entry><type>text</type></entry>
<entry>Delete the field with a specified key, or element with this
value</entry>
<entry><literal>'{"a": "b"}'::jsonb - 'a' </literal></entry>
</row>
<row>
<entry><literal>-</literal></entry>
<entry><type>integer</type></entry>
<entry>Delete the field or element with specified index (Negative
integers count from the end)</entry>
<entry><literal>'["a", "b"]'::jsonb - 1 </literal></entry>
</row>
<row>
<entry><literal>-</literal></entry>
<entry><type>text[]</type></entry>
<entry>Delete the field or element with specified path</entry>
<entry><literal>'["a", {"b":1}]'::jsonb - '{1,b}'::text[] </literal></entry>
</row>
</tbody>
</tgroup>
</table>
@ -10803,6 +10829,42 @@ table2-mapping
<entry><literal>json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')</literal></entry>
<entry><literal>[{"f1":1},2,null,3]</literal></entry>
</row>
<row>
<entry><para><literal>jsonb_replace(target jsonb, path text[], replacement jsonb)</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>.
</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>
</row>
<row>
<entry><para><literal>jsonb_pretty(from_json jsonb)</literal>
</para></entry>
<entry><para><type>text</type></para></entry>
<entry>
Returns <replaceable>from_json</replaceable>
as indented json text.
</entry>
<entry><literal>jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')</literal></entry>
<entry>
<programlisting>
[
{
"f1": 1,
"f2": null
},
2,
null,
3
]
</programlisting>
</entry>
</row>
</tbody>
</tgroup>
</table>