Home Explore Blog CI



postgresql

11th chunk of `doc/src/sgml/json.sgml`
80b868d744c89d3f4c8848bda0572fa38ff4ff41e1e9fa730000000100000fa0
 value is <literal>NULL</literal>, assignment via subscripting will proceed
   as if it was an empty JSON value of the type (object or array) implied by the
   subscript key:

<programlisting>
-- Where jsonb_field was NULL, it is now {"a": 1}
UPDATE table_name SET jsonb_field['a'] = '1';

-- Where jsonb_field was NULL, it is now [1]
UPDATE table_name SET jsonb_field[0] = '1';
</programlisting>

   If an index is specified for an array containing too few elements,
   <literal>NULL</literal> elements will be appended until the index is reachable
   and the value can be set.

<programlisting>
-- Where jsonb_field was [], it is now [null, null, 2];
-- where jsonb_field was [0], it is now [0, null, 2]
UPDATE table_name SET jsonb_field[2] = '2';
</programlisting>

   A <type>jsonb</type> value will accept assignments to nonexistent subscript
   paths as long as the last existing element to be traversed is an object or
   array, as implied by the corresponding subscript (the element indicated by
   the last subscript in the path is not traversed and may be anything). Nested
   array and object structures will be created, and in the former case
   <literal>null</literal>-padded, as specified by the subscript path until the
   assigned value can be placed.

<programlisting>
-- Where jsonb_field was {}, it is now {"a": [{"b": 1}]}
UPDATE table_name SET jsonb_field['a'][0]['b'] = '1';

-- Where jsonb_field was [], it is now [null, {"a": 1}]
UPDATE table_name SET jsonb_field[1]['a'] = '1';
</programlisting>

  </para>
 </sect2>

 <sect2 id="datatype-json-transforms">
  <title>Transforms</title>

  <para>
   Additional extensions are available that implement transforms for the
   <type>jsonb</type> type for different procedural languages.
  </para>

  <para>
   The extensions for PL/Perl are called <literal>jsonb_plperl</literal> and
   <literal>jsonb_plperlu</literal>.  If you use them, <type>jsonb</type>
   values are mapped to Perl arrays, hashes, and scalars, as appropriate.
  </para>

  <para>
   The extension for PL/Python is called <literal>jsonb_plpython3u</literal>.
   If you use it, <type>jsonb</type> values are mapped to Python
   dictionaries, lists, and scalars, as appropriate.
  </para>

  <para>
   Of these extensions, <literal>jsonb_plperl</literal> is
   considered <quote>trusted</quote>, that is, it can be installed by
   non-superusers who have <literal>CREATE</literal> privilege on the
   current database.  The rest require superuser privilege to install.
  </para>
 </sect2>

 <sect2 id="datatype-jsonpath">
  <title>jsonpath Type</title>

  <indexterm zone="datatype-jsonpath">
   <primary>jsonpath</primary>
  </indexterm>

  <para>
   The <type>jsonpath</type> type implements support for the SQL/JSON path language
   in <productname>PostgreSQL</productname> to efficiently query JSON data.
   It provides a binary representation of the parsed SQL/JSON path
   expression that specifies the items to be retrieved by the path
   engine from the JSON data for further processing with the
   SQL/JSON query functions.
  </para>

  <para>
   The semantics of SQL/JSON path predicates and operators generally follow SQL.
   At the same time, to provide a natural way of working with JSON data,
   SQL/JSON path syntax uses some JavaScript conventions:
  </para>

  <itemizedlist>
   <listitem>
    <para>
     Dot (<literal>.</literal>) is used for member access.
    </para>
   </listitem>
   <listitem>
    <para>
     Square brackets (<literal>[]</literal>) are used for array access.
    </para>
   </listitem>
   <listitem>
    <para>
     SQL/JSON arrays are 0-relative, unlike regular SQL arrays that start from 1.
    </para>
   </listitem>
  </itemizedlist>

  <para>
   Numeric literals in SQL/JSON path expressions follow JavaScript rules,
   which are different from both SQL and JSON in some minor details.  For
   example, SQL/JSON path allows <literal>.1</literal> and
   <literal>1.</literal>, which are invalid in JSON.  Non-decimal

Title: JSONB Transforms and JSONPath
Summary
PostgreSQL provides extensions for transforms of the jsonb type for different procedural languages, such as PL/Perl and PL/Python, and also supports the SQL/JSON path language through the jsonpath type, which allows efficient querying of JSON data using a binary representation of the parsed SQL/JSON path expression.