<!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.6.4 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="2017-08-11T02:27:18"></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.6.4 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-42</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-42. <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" >-></TT ></TD ><TD ><TT CLASS="TYPE" >int</TT ></TD ><TD >Get JSON array element (indexed from zero, negative integers count from the end)</TD ><TD ><TT CLASS="LITERAL" >'[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2</TT ></TD ><TD ><TT CLASS="LITERAL" >{"c":"baz"}</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >-></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->'a'</TT ></TD ><TD ><TT CLASS="LITERAL" >{"b":"foo"}</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >->></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->>2</TT ></TD ><TD ><TT CLASS="LITERAL" >3</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >->></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->>'b'</TT ></TD ><TD ><TT CLASS="LITERAL" >2</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >#></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#>'{a,b}'</TT ></TD ><TD ><TT CLASS="LITERAL" >{"c": "foo"}</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >#>></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#>>'{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. The field/element/path extraction operators that accept integer JSON array subscripts all support negative subscripting from the end of arrays. </P ></BLOCKQUOTE ></DIV ><P > The standard comparison operators shown in <A HREF="functions-comparison.html#FUNCTIONS-COMPARISON-OP-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-43</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-43. 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" >@></TT ></TD ><TD ><TT CLASS="TYPE" >jsonb</TT ></TD ><TD >Does the left JSON value contain the right JSON path/value entries at the top level?</TD ><TD ><TT CLASS="LITERAL" >'{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" ><@</TT ></TD ><TD ><TT CLASS="TYPE" >jsonb</TT ></TD ><TD >Are the left JSON path/value entries contained at the top level within the right JSON value?</TD ><TD ><TT CLASS="LITERAL" >'{"b":2}'::jsonb <@ '{"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 <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >string</I ></SPAN > exist as a top-level key 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 array <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >strings</I ></SPAN > exist as top-level keys?</TD ><TD ><TT CLASS="LITERAL" >'{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >?&</TT ></TD ><TD ><TT CLASS="TYPE" >text[]</TT ></TD ><TD >Do all of these array <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >strings</I ></SPAN > exist as top-level keys?</TD ><TD ><TT CLASS="LITERAL" >'["a", "b"]'::jsonb ?& array['a', 'b']</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >||</TT ></TD ><TD ><TT CLASS="TYPE" >jsonb</TT ></TD ><TD >Concatenate two <TT CLASS="TYPE" >jsonb</TT > values into a new <TT CLASS="TYPE" >jsonb</TT > value</TD ><TD ><TT CLASS="LITERAL" >'["a", "b"]'::jsonb || '["c", "d"]'::jsonb</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >-</TT ></TD ><TD ><TT CLASS="TYPE" >text</TT ></TD ><TD >Delete key/value pair or <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >string</I ></SPAN > element from left operand. Key/value pairs are matched based on their key value.</TD ><TD ><TT CLASS="LITERAL" >'{"a": "b"}'::jsonb - 'a' </TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >-</TT ></TD ><TD ><TT CLASS="TYPE" >integer</TT ></TD ><TD >Delete the array element with specified index (Negative integers count from the end). Throws an error if top level container is not an array.</TD ><TD ><TT CLASS="LITERAL" >'["a", "b"]'::jsonb - 1 </TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >#-</TT ></TD ><TD ><TT CLASS="TYPE" >text[]</TT ></TD ><TD >Delete the field or element with specified path (for JSON arrays, negative integers count from the end)</TD ><TD ><TT CLASS="LITERAL" >'["a", {"b":1}]'::jsonb #- '{1,b}'</TT ></TD ></TR ></TBODY ></TABLE ></DIV ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > The <TT CLASS="LITERAL" >||</TT > 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. </P ></BLOCKQUOTE ></DIV ><P > <A HREF="functions-json.html#FUNCTIONS-JSON-CREATION-TABLE" >Table 9-44</A > shows the functions that are available for creating <TT CLASS="TYPE" >json</TT > and <TT CLASS="TYPE" >jsonb</TT > values. (There are no equivalent functions for <TT CLASS="TYPE" >jsonb</TT >, of the <TT CLASS="LITERAL" >row_to_json</TT > and <TT CLASS="LITERAL" >array_to_json</TT > functions. However, the <TT CLASS="LITERAL" >to_jsonb</TT > function supplies much the same functionality as these functions would.) </P ><DIV CLASS="TABLE" ><A NAME="FUNCTIONS-JSON-CREATION-TABLE" ></A ><P ><B >Table 9-44. 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 ><P ><TT CLASS="LITERAL" >to_json(anyelement)</TT > </P ><P ><TT CLASS="LITERAL" >to_jsonb(anyelement)</TT > </P ></TD ><TD > Returns the value as <TT CLASS="TYPE" >json</TT > or <TT CLASS="TYPE" >jsonb</TT >. 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 scalar value is produced. For any scalar type other than a number, a Boolean, or a null value, the text representation will be used, in such a fashion that it is a valid <TT CLASS="TYPE" >json</TT > or <TT CLASS="TYPE" >jsonb</TT > value. </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 ><P ><TT CLASS="LITERAL" >json_build_array(VARIADIC "any")</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_build_array(VARIADIC "any")</TT > </P ></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 ><P ><TT CLASS="LITERAL" >json_build_object(VARIADIC "any")</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_build_object(VARIADIC "any")</TT > </P ></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 ><P ><TT CLASS="LITERAL" >json_object(text[])</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_object(text[])</TT > </P ></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 ><P ><TT CLASS="LITERAL" >json_object(keys text[], values text[])</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_object(keys text[], values text[])</TT > </P ></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-45</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-45. 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" >#></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" >#>></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 ><TR ><TD ><P ><TT CLASS="LITERAL" >json_strip_nulls(from_json json)</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_strip_nulls(from_json jsonb)</TT > </P ></TD ><TD ><P ><TT CLASS="TYPE" >json</TT ></P ><P ><TT CLASS="TYPE" >jsonb</TT ></P ></TD ><TD > Returns <TT CLASS="REPLACEABLE" ><I >from_json</I ></TT > with all object fields that have null values omitted. Other null values are untouched. </TD ><TD ><TT CLASS="LITERAL" >json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')</TT ></TD ><TD ><TT CLASS="LITERAL" >[{"f1":1},2,null,3]</TT ></TD ></TR ><TR ><TD ><P ><TT CLASS="LITERAL" >jsonb_set(target jsonb, path text[], new_value jsonb[<SPAN CLASS="OPTIONAL" >, <TT CLASS="PARAMETER" >create_missing</TT > <TT CLASS="TYPE" >boolean</TT ></SPAN >])</TT > </P ></TD ><TD ><P ><TT CLASS="TYPE" >jsonb</TT ></P ></TD ><TD > Returns <TT CLASS="REPLACEABLE" ><I >target</I ></TT > with the section designated by <TT CLASS="REPLACEABLE" ><I >path</I ></TT > replaced by <TT CLASS="REPLACEABLE" ><I >new_value</I ></TT >, or with <TT CLASS="REPLACEABLE" ><I >new_value</I ></TT > added if <TT CLASS="REPLACEABLE" ><I >create_missing</I ></TT > is true ( default is <TT CLASS="LITERAL" >true</TT >) and the item designated by <TT CLASS="REPLACEABLE" ><I >path</I ></TT > does not exist. As with the path orientated operators, negative integers that appear in <TT CLASS="REPLACEABLE" ><I >path</I ></TT > count from the end of JSON arrays. </TD ><TD ><P ><TT CLASS="LITERAL" >jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)</TT > </P ><P ><TT CLASS="LITERAL" >jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')</TT > </P ></TD ><TD ><P ><TT CLASS="LITERAL" >[{"f1":[2,3,4],"f2":null},2,null,3]</TT > </P ><P ><TT CLASS="LITERAL" >[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]</TT > </P ></TD ></TR ><TR ><TD > <P ><TT CLASS="LITERAL" > jsonb_insert(target jsonb, path text[], new_value jsonb, [<SPAN CLASS="OPTIONAL" ><TT CLASS="PARAMETER" >insert_after</TT > <TT CLASS="TYPE" >boolean</TT ></SPAN >]) </TT ></P > </TD ><TD ><P ><TT CLASS="TYPE" >jsonb</TT ></P ></TD ><TD > Returns <TT CLASS="REPLACEABLE" ><I >target</I ></TT > with <TT CLASS="REPLACEABLE" ><I >new_value</I ></TT > inserted. If <TT CLASS="REPLACEABLE" ><I >target</I ></TT > section designated by <TT CLASS="REPLACEABLE" ><I >path</I ></TT > is in a JSONB array, <TT CLASS="REPLACEABLE" ><I >new_value</I ></TT > will be inserted before target or after if <TT CLASS="REPLACEABLE" ><I >insert_after</I ></TT > is true (default is <TT CLASS="LITERAL" >false</TT >). If <TT CLASS="REPLACEABLE" ><I >target</I ></TT > section designated by <TT CLASS="REPLACEABLE" ><I >path</I ></TT > is in JSONB object, <TT CLASS="REPLACEABLE" ><I >new_value</I ></TT > will be inserted only if <TT CLASS="REPLACEABLE" ><I >target</I ></TT > does not exist. As with the path orientated operators, negative integers that appear in <TT CLASS="REPLACEABLE" ><I >path</I ></TT > count from the end of JSON arrays. </TD ><TD > <P ><TT CLASS="LITERAL" > jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"') </TT ></P > <P ><TT CLASS="LITERAL" > jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true) </TT ></P > </TD ><TD ><P ><TT CLASS="LITERAL" >{"a": [0, "new_value", 1, 2]}</TT > </P ><P ><TT CLASS="LITERAL" >{"a": [0, 1, "new_value", 2]}</TT > </P ></TD ></TR ><TR ><TD ><P ><TT CLASS="LITERAL" >jsonb_pretty(from_json jsonb)</TT > </P ></TD ><TD ><P ><TT CLASS="TYPE" >text</TT ></P ></TD ><TD > Returns <TT CLASS="REPLACEABLE" ><I >from_json</I ></TT > as indented JSON text. </TD ><TD ><TT CLASS="LITERAL" >jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')</TT ></TD ><TD ><PRE CLASS="PROGRAMLISTING" >[ { "f1": 1, "f2": null }, 2, null, 3 ]</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 > In <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 >, type coercion from the JSON is <SPAN CLASS="QUOTE" >"best effort"</SPAN > and may not result in desired values for some types. JSON keys are matched to identical column names in the target row type. 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 > All the items of the <TT CLASS="LITERAL" >path</TT > parameter of <TT CLASS="LITERAL" >jsonb_set</TT > as well as <TT CLASS="LITERAL" >jsonb_insert</TT > except the last item must be present in the <TT CLASS="LITERAL" >target</TT >. If <TT CLASS="LITERAL" >create_missing</TT > is false, all items of the <TT CLASS="LITERAL" >path</TT > parameter of <TT CLASS="LITERAL" >jsonb_set</TT > must be present. If these conditions are not met the <TT CLASS="LITERAL" >target</TT > is returned unchanged. </P ><P > 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 - <TT CLASS="LITERAL" >-1</TT > 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. </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 ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > If the argument to <TT CLASS="LITERAL" >json_strip_nulls</TT > contains duplicate field names in any object, the result could be semantically somewhat different, depending on the order in which they occur. This is not an issue for <TT CLASS="LITERAL" >jsonb_strip_nulls</TT > since <TT CLASS="TYPE" >jsonb</TT > values never have duplicate object field names. </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, and their <TT CLASS="TYPE" >jsonb</TT > equivalents, <CODE CLASS="FUNCTION" >jsonb_agg</CODE > and <CODE CLASS="FUNCTION" >jsonb_object_agg</CODE >. </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 >