Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 78943537a03eb3bb165d73ab4fd2d713 > files > 281

postgresql9.4-docs-9.4.24-1.mga6.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>JSON Functions and Operators</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.4.24 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Functions and Operators"
HREF="functions.html"><LINK
REL="PREVIOUS"
TITLE="XML Functions"
HREF="functions-xml.html"><LINK
REL="NEXT"
TITLE="Sequence Manipulation Functions"
HREF="functions-sequence.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2019-08-11T16:12:25"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="4"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.4.24 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="XML Functions"
HREF="functions-xml.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 9. Functions and Operators</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Sequence Manipulation Functions"
HREF="functions-sequence.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="FUNCTIONS-JSON"
>9.15. JSON Functions and Operators</A
></H1
><P
>   <A
HREF="functions-json.html#FUNCTIONS-JSON-OP-TABLE"
>Table 9-40</A
> shows the operators that
   are available for use with the two JSON data types (see <A
HREF="datatype-json.html"
>Section 8.14</A
>).
  </P
><DIV
CLASS="TABLE"
><A
NAME="FUNCTIONS-JSON-OP-TABLE"
></A
><P
><B
>Table 9-40. <TT
CLASS="TYPE"
>json</TT
> and <TT
CLASS="TYPE"
>jsonb</TT
> Operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><COL><THEAD
><TR
><TH
>Operator</TH
><TH
>Right Operand Type</TH
><TH
>Description</TH
><TH
>Example</TH
><TH
>Example Result</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>-&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>int</TT
></TD
><TD
>Get JSON array element (indexed from zero)</TD
><TD
><TT
CLASS="LITERAL"
>'[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json-&gt;2</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"c":"baz"}</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>Get JSON object field by key</TD
><TD
><TT
CLASS="LITERAL"
>'{"a": {"b":"foo"}}'::json-&gt;'a'</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"b":"foo"}</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-&gt;&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>int</TT
></TD
><TD
>Get JSON array element as <TT
CLASS="TYPE"
>text</TT
></TD
><TD
><TT
CLASS="LITERAL"
>'[1,2,3]'::json-&gt;&gt;2</TT
></TD
><TD
><TT
CLASS="LITERAL"
>3</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-&gt;&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>Get JSON object field as <TT
CLASS="TYPE"
>text</TT
></TD
><TD
><TT
CLASS="LITERAL"
>'{"a":1,"b":2}'::json-&gt;&gt;'b'</TT
></TD
><TD
><TT
CLASS="LITERAL"
>2</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>#&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>text[]</TT
></TD
><TD
>Get JSON object at specified path</TD
><TD
><TT
CLASS="LITERAL"
>'{"a": {"b":{"c": "foo"}}}'::json#&gt;'{a,b}'</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"c": "foo"}</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>#&gt;&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>text[]</TT
></TD
><TD
>Get JSON object at specified path as <TT
CLASS="TYPE"
>text</TT
></TD
><TD
><TT
CLASS="LITERAL"
>'{"a":[1,2,3],"b":[4,5,6]}'::json#&gt;&gt;'{a,2}'</TT
></TD
><TD
><TT
CLASS="LITERAL"
>3</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>    There are parallel variants of these operators for both the
    <TT
CLASS="TYPE"
>json</TT
> and <TT
CLASS="TYPE"
>jsonb</TT
> types.
    The field/element/path extraction operators
    return the same type as their left-hand input (either <TT
CLASS="TYPE"
>json</TT
>
    or <TT
CLASS="TYPE"
>jsonb</TT
>), except for those specified as
    returning <TT
CLASS="TYPE"
>text</TT
>, which coerce the value to text.
    The field/element/path extraction operators return NULL, rather than
    failing, if the JSON input does not have the right structure to match
    the request; for example if no such element exists.
   </P
></BLOCKQUOTE
></DIV
><P
>   The standard comparison operators shown in  <A
HREF="functions-comparison.html#FUNCTIONS-COMPARISON-TABLE"
>Table 9-1</A
> are available for
   <TT
CLASS="TYPE"
>jsonb</TT
>, but not for <TT
CLASS="TYPE"
>json</TT
>. They follow the
   ordering rules for B-tree operations outlined at <A
HREF="datatype-json.html#JSON-INDEXING"
>Section 8.14.4</A
>.
  </P
><P
>   Some further operators also exist only for <TT
CLASS="TYPE"
>jsonb</TT
>, as shown
   in <A
HREF="functions-json.html#FUNCTIONS-JSONB-OP-TABLE"
>Table 9-41</A
>.
   Many of these operators can be indexed by
   <TT
CLASS="TYPE"
>jsonb</TT
> operator classes.  For a full description of
   <TT
CLASS="TYPE"
>jsonb</TT
> containment and existence semantics, see <A
HREF="datatype-json.html#JSON-CONTAINMENT"
>Section 8.14.3</A
>.  <A
HREF="datatype-json.html#JSON-INDEXING"
>Section 8.14.4</A
>
   describes how these operators can be used to effectively index
   <TT
CLASS="TYPE"
>jsonb</TT
> data.
  </P
><DIV
CLASS="TABLE"
><A
NAME="FUNCTIONS-JSONB-OP-TABLE"
></A
><P
><B
>Table 9-41. Additional <TT
CLASS="TYPE"
>jsonb</TT
> Operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><THEAD
><TR
><TH
>Operator</TH
><TH
>Right Operand Type</TH
><TH
>Description</TH
><TH
>Example</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>@&gt;</TT
></TD
><TD
><TT
CLASS="TYPE"
>jsonb</TT
></TD
><TD
>Does the left JSON value contain within it the right value?</TD
><TD
><TT
CLASS="LITERAL"
>'{"a":1, "b":2}'::jsonb @&gt; '{"b":2}'::jsonb</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>&lt;@</TT
></TD
><TD
><TT
CLASS="TYPE"
>jsonb</TT
></TD
><TD
>Is the left JSON value contained within the right value?</TD
><TD
><TT
CLASS="LITERAL"
>'{"b":2}'::jsonb &lt;@ '{"a":1, "b":2}'::jsonb</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>?</TT
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>Does the key/element <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>string</I
></SPAN
> exist within
        the JSON value?</TD
><TD
><TT
CLASS="LITERAL"
>'{"a":1, "b":2}'::jsonb ? 'b'</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>?|</TT
></TD
><TD
><TT
CLASS="TYPE"
>text[]</TT
></TD
><TD
>Do any of these key/element <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>strings</I
></SPAN
> exist?</TD
><TD
><TT
CLASS="LITERAL"
>'{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>?&amp;</TT
></TD
><TD
><TT
CLASS="TYPE"
>text[]</TT
></TD
><TD
>Do all of these key/element <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>strings</I
></SPAN
> exist?</TD
><TD
><TT
CLASS="LITERAL"
>'["a", "b"]'::jsonb ?&amp; array['a', 'b']</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><P
>   <A
HREF="functions-json.html#FUNCTIONS-JSON-CREATION-TABLE"
>Table 9-42</A
> shows the functions that are
   available for creating <TT
CLASS="TYPE"
>json</TT
> values.
   (Currently, there are no equivalent functions for <TT
CLASS="TYPE"
>jsonb</TT
>, but you
   can cast the result of one of these functions to <TT
CLASS="TYPE"
>jsonb</TT
>.)
  </P
><DIV
CLASS="TABLE"
><A
NAME="FUNCTIONS-JSON-CREATION-TABLE"
></A
><P
><B
>Table 9-42. JSON Creation Functions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><THEAD
><TR
><TH
>Function</TH
><TH
>Description</TH
><TH
>Example</TH
><TH
>Example Result</TH
></TR
></THEAD
><TBODY
><TR
><TD
>         <TT
CLASS="LITERAL"
>to_json(anyelement)</TT
>
       </TD
><TD
>         Returns the value as JSON.  Arrays and composites are converted
         (recursively) to arrays and objects; otherwise, if there is a cast
         from the type to <TT
CLASS="TYPE"
>json</TT
>, the cast function will be used to
         perform the conversion; otherwise, a JSON scalar value is produced.
         For any scalar type other than a number, a Boolean, or a null value,
         the text representation will be used, properly quoted and escaped
         so that it is a valid JSON string.
       </TD
><TD
><TT
CLASS="LITERAL"
>to_json('Fred said "Hi."'::text)</TT
></TD
><TD
><TT
CLASS="LITERAL"
>"Fred said \"Hi.\""</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>array_to_json(anyarray [, pretty_bool])</TT
>
       </TD
><TD
>         Returns the array as a JSON array. A PostgreSQL multidimensional array
         becomes a JSON array of arrays. Line feeds will be added between
         dimension-1 elements if <TT
CLASS="PARAMETER"
>pretty_bool</TT
> is true.
       </TD
><TD
><TT
CLASS="LITERAL"
>array_to_json('{{1,5},{99,100}}'::int[])</TT
></TD
><TD
><TT
CLASS="LITERAL"
>[[1,5],[99,100]]</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>row_to_json(record [, pretty_bool])</TT
>
       </TD
><TD
>         Returns the row as a JSON object. Line feeds will be added between
         level-1 elements if <TT
CLASS="PARAMETER"
>pretty_bool</TT
> is true.
       </TD
><TD
><TT
CLASS="LITERAL"
>row_to_json(row(1,'foo'))</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"f1":1,"f2":"foo"}</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>json_build_array(VARIADIC "any")</TT
>
       </TD
><TD
>         Builds a possibly-heterogeneously-typed JSON array out of a variadic
         argument list.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_build_array(1,2,'3',4,5)</TT
></TD
><TD
><TT
CLASS="LITERAL"
>[1, 2, "3", 4, 5]</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>json_build_object(VARIADIC "any")</TT
>
       </TD
><TD
>         Builds a JSON object out of a variadic argument list.  By
         convention, the argument list consists of alternating
         keys and values.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_build_object('foo',1,'bar',2)</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"foo": 1, "bar": 2}</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>json_object(text[])</TT
>
       </TD
><TD
>         Builds a JSON object out of a text array.  The array must have either
         exactly one dimension with an even number of members, in which case
         they are taken as alternating key/value pairs, or two dimensions
         such that each inner array has exactly two elements, which
         are taken as a key/value pair.
       </TD
><TD
><P
><TT
CLASS="LITERAL"
>json_object('{a, 1, b, "def", c, 3.5}')</TT
></P
>
        <P
><TT
CLASS="LITERAL"
>json_object('{{a, 1},{b, "def"},{c, 3.5}}')</TT
></P
></TD
><TD
><TT
CLASS="LITERAL"
>{"a": "1", "b": "def", "c": "3.5"}</TT
></TD
></TR
><TR
><TD
>         <TT
CLASS="LITERAL"
>json_object(keys text[], values text[])</TT
>
       </TD
><TD
>         This form of <CODE
CLASS="FUNCTION"
>json_object</CODE
> takes keys and values pairwise from two separate
         arrays. In all other respects it is identical to the one-argument form.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_object('{a, b}', '{1,2}')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"a": "1", "b": "2"}</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>     <CODE
CLASS="FUNCTION"
>array_to_json</CODE
> and <CODE
CLASS="FUNCTION"
>row_to_json</CODE
> have the same
     behavior as <CODE
CLASS="FUNCTION"
>to_json</CODE
> except for offering a pretty-printing
     option.  The behavior described for <CODE
CLASS="FUNCTION"
>to_json</CODE
> likewise applies
     to each individual value converted by the other JSON creation functions.
    </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>     The <A
HREF="hstore.html"
>hstore</A
> extension has a cast
     from <TT
CLASS="TYPE"
>hstore</TT
> to <TT
CLASS="TYPE"
>json</TT
>, so that
     <TT
CLASS="TYPE"
>hstore</TT
> values converted via the JSON creation functions
     will be represented as JSON objects, not as primitive string values.
    </P
></BLOCKQUOTE
></DIV
><P
>   <A
HREF="functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE"
>Table 9-43</A
> shows the functions that
   are available for processing <TT
CLASS="TYPE"
>json</TT
> and <TT
CLASS="TYPE"
>jsonb</TT
> values.
  </P
><DIV
CLASS="TABLE"
><A
NAME="FUNCTIONS-JSON-PROCESSING-TABLE"
></A
><P
><B
>Table 9-43. JSON Processing Functions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><COL><THEAD
><TR
><TH
>Function</TH
><TH
>Return Type</TH
><TH
>Description</TH
><TH
>Example</TH
><TH
>Example Result</TH
></TR
></THEAD
><TBODY
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_array_length(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_array_length(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>int</TT
></TD
><TD
>         Returns the number of elements in the outermost JSON array.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>5</TT
></TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_each(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_each(jsonb)</TT
>
       </P
></TD
><TD
><P
><TT
CLASS="LITERAL"
>setof key text, value json</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>setof key text, value jsonb</TT
>
       </P
></TD
><TD
>         Expands the outermost JSON object into a set of key/value pairs.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_each('{"a":"foo", "b":"bar"}')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> key | value
-----+-------
 a   | "foo"
 b   | "bar"</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_each_text(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_each_text(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>setof key text, value text</TT
></TD
><TD
>         Expands the outermost JSON object into a set of key/value pairs. The
         returned values will be of type <TT
CLASS="TYPE"
>text</TT
>.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_each_text('{"a":"foo", "b":"bar"}')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> key | value
-----+-------
 a   | foo
 b   | bar</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_extract_path(from_json json, VARIADIC path_elems text[])</TT
>
        </P
><P
><TT
CLASS="LITERAL"
>jsonb_extract_path(from_json jsonb, VARIADIC path_elems text[])</TT
>
       </P
></TD
><TD
><P
><TT
CLASS="TYPE"
>json</TT
></P
><P
><TT
CLASS="TYPE"
>jsonb</TT
>
       </P
></TD
><TD
>         Returns JSON value pointed to by <TT
CLASS="REPLACEABLE"
><I
>path_elems</I
></TT
>
         (equivalent to <TT
CLASS="LITERAL"
>#&gt;</TT
> operator).
       </TD
><TD
><TT
CLASS="LITERAL"
>json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>{"f5":99,"f6":"foo"}</TT
></TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_extract_path_text(from_json json, VARIADIC path_elems text[])</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[])</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>         Returns JSON value pointed to by <TT
CLASS="REPLACEABLE"
><I
>path_elems</I
></TT
>
         as <TT
CLASS="TYPE"
>text</TT
>
         (equivalent to <TT
CLASS="LITERAL"
>#&gt;&gt;</TT
> operator).
       </TD
><TD
><TT
CLASS="LITERAL"
>json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>foo</TT
></TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_object_keys(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_object_keys(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>setof text</TT
></TD
><TD
>          Returns set of keys in the outermost JSON object.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> json_object_keys
------------------
 f1
 f2</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_populate_record(base anyelement, from_json json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_populate_record(base anyelement, from_json jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>anyelement</TT
></TD
><TD
>         Expands the object in <TT
CLASS="REPLACEABLE"
><I
>from_json</I
></TT
> to a row
         whose columns match the record type defined by <TT
CLASS="REPLACEABLE"
><I
>base</I
></TT
>
         (see note below).
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_populate_record(null::myrowtype, '{"a":1,"b":2}')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> a | b
---+---
 1 | 2</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_populate_recordset(base anyelement, from_json json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_populate_recordset(base anyelement, from_json jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>setof anyelement</TT
></TD
><TD
>         Expands the outermost array of objects
         in <TT
CLASS="REPLACEABLE"
><I
>from_json</I
></TT
> to a set of rows whose
         columns match the record type defined by <TT
CLASS="REPLACEABLE"
><I
>base</I
></TT
> (see
         note below).
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> a | b
---+---
 1 | 2
 3 | 4</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_array_elements(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_array_elements(jsonb)</TT
>
       </P
></TD
><TD
><P
><TT
CLASS="TYPE"
>setof json</TT
>
         </P
><P
><TT
CLASS="TYPE"
>setof jsonb</TT
>
       </P
></TD
><TD
>         Expands a JSON array to a set of JSON values.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_array_elements('[1,true, [2,false]]')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
>   value
-----------
 1
 true
 [2,false]</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_array_elements_text(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_array_elements_text(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>setof text</TT
></TD
><TD
>         Expands a JSON array to a set of <TT
CLASS="TYPE"
>text</TT
> values.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_array_elements_text('["foo", "bar"]')</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
>   value
-----------
 foo
 bar</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_typeof(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_typeof(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>         Returns the type of the outermost JSON value as a text string.
         Possible types are
         <TT
CLASS="LITERAL"
>object</TT
>, <TT
CLASS="LITERAL"
>array</TT
>, <TT
CLASS="LITERAL"
>string</TT
>, <TT
CLASS="LITERAL"
>number</TT
>,
         <TT
CLASS="LITERAL"
>boolean</TT
>, and <TT
CLASS="LITERAL"
>null</TT
>.
       </TD
><TD
><TT
CLASS="LITERAL"
>json_typeof('-123.4')</TT
></TD
><TD
><TT
CLASS="LITERAL"
>number</TT
></TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_to_record(json)</TT
>
          </P
><P
><TT
CLASS="LITERAL"
>jsonb_to_record(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>record</TT
></TD
><TD
>         Builds an arbitrary record from a JSON object (see note below).  As
         with all functions returning <TT
CLASS="TYPE"
>record</TT
>, the caller must
         explicitly define the structure of the record with an <TT
CLASS="LITERAL"
>AS</TT
>
         clause.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}') as x(a int, b text, d text) </TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> a |    b    | d
---+---------+---
 1 | [1,2,3] |</PRE
>
       </TD
></TR
><TR
><TD
><P
><TT
CLASS="LITERAL"
>json_to_recordset(json)</TT
>
         </P
><P
><TT
CLASS="LITERAL"
>jsonb_to_recordset(jsonb)</TT
>
       </P
></TD
><TD
><TT
CLASS="TYPE"
>setof record</TT
></TD
><TD
>         Builds an arbitrary set of records from a JSON array of objects (see
         note below).  As with all functions returning <TT
CLASS="TYPE"
>record</TT
>, the
         caller must explicitly define the structure of the record with
         an <TT
CLASS="LITERAL"
>AS</TT
> clause.
       </TD
><TD
><TT
CLASS="LITERAL"
>select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text);</TT
></TD
><TD
><PRE
CLASS="PROGRAMLISTING"
> a |  b
---+-----
 1 | foo
 2 |</PRE
>
       </TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>      Many of these functions and operators will convert Unicode escapes in
      JSON strings to the appropriate single character.  This is a non-issue
      if the input is type <TT
CLASS="TYPE"
>jsonb</TT
>, because the conversion was already
      done; but for <TT
CLASS="TYPE"
>json</TT
> input, this may result in throwing an error,
      as noted in <A
HREF="datatype-json.html"
>Section 8.14</A
>.
    </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>      While the examples for the functions
      <CODE
CLASS="FUNCTION"
>json_populate_record</CODE
>,
      <CODE
CLASS="FUNCTION"
>json_populate_recordset</CODE
>,
      <CODE
CLASS="FUNCTION"
>json_to_record</CODE
> and
      <CODE
CLASS="FUNCTION"
>json_to_recordset</CODE
> use constants, the typical use
      would be to reference a table in the <TT
CLASS="LITERAL"
>FROM</TT
> clause
      and use one of its <TT
CLASS="TYPE"
>json</TT
> or <TT
CLASS="TYPE"
>jsonb</TT
> columns
      as an argument to the function.  Extracted key values can then be
      referenced in other parts of the query, like <TT
CLASS="LITERAL"
>WHERE</TT
>
      clauses and target lists.  Extracting multiple values in this
      way can improve performance over extracting them separately with
      per-key operators.
    </P
><P
>      JSON keys are matched to identical column names in the target
      row type.  JSON type coercion for these functions is <SPAN
CLASS="QUOTE"
>"best
      effort"</SPAN
> and may not result in desired values for some types.
      JSON fields that do not appear in the target row type will be
      omitted from the output, and target columns that do not match any
      JSON field will simply be NULL.
  </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>      The <TT
CLASS="LITERAL"
>json_typeof</TT
> function's <TT
CLASS="LITERAL"
>null</TT
> return value
      should not be confused with a SQL NULL.  While
      calling <TT
CLASS="LITERAL"
>json_typeof('null'::json)</TT
> will
      return <TT
CLASS="LITERAL"
>null</TT
>, calling <TT
CLASS="LITERAL"
>json_typeof(NULL::json)</TT
>
      will return a SQL NULL.
    </P
></BLOCKQUOTE
></DIV
><P
>    See also <A
HREF="functions-aggregate.html"
>Section 9.20</A
> for the aggregate
    function <CODE
CLASS="FUNCTION"
>json_agg</CODE
> which aggregates record
    values as JSON, and the aggregate function
    <CODE
CLASS="FUNCTION"
>json_object_agg</CODE
> which aggregates pairs of values
    into a JSON object.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions-xml.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions-sequence.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>XML Functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Sequence Manipulation Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>