@@ -10106,14 +10106,14 @@ table2-mapping
10106
10106
<row>
10107
10107
<entry><literal>-></literal></entry>
10108
10108
<entry><type>int</type></entry>
10109
- <entry>Get JSON array element</entry>
10110
- <entry><literal>'[{"a":"foo"},{"a ":"bar"},{"a ":"baz"}]'::json->2</literal></entry>
10111
- <entry><literal>{"a ":"baz"}</literal></entry>
10109
+ <entry>Get JSON array element (indexed from zero) </entry>
10110
+ <entry><literal>'[{"a":"foo"},{"b ":"bar"},{"c ":"baz"}]'::json->2</literal></entry>
10111
+ <entry><literal>{"c ":"baz"}</literal></entry>
10112
10112
</row>
10113
10113
<row>
10114
10114
<entry><literal>-></literal></entry>
10115
10115
<entry><type>text</type></entry>
10116
- <entry>Get JSON object field</entry>
10116
+ <entry>Get JSON object field by key </entry>
10117
10117
<entry><literal>'{"a": {"b":"foo"}}'::json->'a'</literal></entry>
10118
10118
<entry><literal>{"b":"foo"}</literal></entry>
10119
10119
</row>
@@ -10134,7 +10134,7 @@ table2-mapping
10134
10134
<row>
10135
10135
<entry><literal>#></literal></entry>
10136
10136
<entry><type>text[]</type></entry>
10137
- <entry>Get JSON object at specified path</entry>
10137
+ <entry>Get JSON object at specified path</entry>
10138
10138
<entry><literal>'{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'</literal></entry>
10139
10139
<entry><literal>{"c": "foo"}</literal></entry>
10140
10140
</row>
@@ -10164,10 +10164,10 @@ table2-mapping
10164
10164
in <xref linkend="functions-jsonb-op-table">.
10165
10165
Many of these operators can be indexed by
10166
10166
<type>jsonb</> operator classes. For a full description of
10167
- <type>jsonb</> containment semantics and nesting , see <xref
10167
+ <type>jsonb</> containment and existence semantics , see <xref
10168
10168
linkend="json-containment">. <xref linkend="json-indexing">
10169
10169
describes how these operators can be used to effectively index
10170
- <type>jsonb</>.
10170
+ <type>jsonb</> data .
10171
10171
</para>
10172
10172
<table id="functions-jsonb-op-table">
10173
10173
<title>Additional <type>jsonb</> Operators</title>
@@ -10230,13 +10230,13 @@ table2-mapping
10230
10230
</para>
10231
10231
10232
10232
<indexterm>
10233
- <primary>array_to_json </primary>
10233
+ <primary>to_json </primary>
10234
10234
</indexterm>
10235
10235
<indexterm>
10236
- <primary>row_to_json </primary>
10236
+ <primary>array_to_json </primary>
10237
10237
</indexterm>
10238
10238
<indexterm>
10239
- <primary>to_json </primary>
10239
+ <primary>row_to_json </primary>
10240
10240
</indexterm>
10241
10241
<indexterm>
10242
10242
<primary>json_build_array</primary>
@@ -10260,14 +10260,30 @@ table2-mapping
10260
10260
</row>
10261
10261
</thead>
10262
10262
<tbody>
10263
+ <row>
10264
+ <entry>
10265
+ <literal>to_json(anyelement)</literal>
10266
+ </entry>
10267
+ <entry>
10268
+ Returns the value as JSON. Arrays and composites are converted
10269
+ (recursively) to arrays and objects; otherwise, if there is a cast
10270
+ from the type to <type>json</type>, the cast function will be used to
10271
+ perform the conversion; otherwise, a JSON scalar value is produced.
10272
+ For any scalar type other than a number, a boolean, or a null value,
10273
+ the text representation will be used, properly quoted and escaped
10274
+ so that it is a valid JSON string.
10275
+ </entry>
10276
+ <entry><literal>to_json('Fred said "Hi."'::text)</literal></entry>
10277
+ <entry><literal>"Fred said \"Hi.\""</literal></entry>
10278
+ </row>
10263
10279
<row>
10264
10280
<entry>
10265
10281
<literal>array_to_json(anyarray [, pretty_bool])</literal>
10266
10282
</entry>
10267
10283
<entry>
10268
- Returns the array as JSON. A PostgreSQL multidimensional array
10284
+ Returns the array as a JSON array . A PostgreSQL multidimensional array
10269
10285
becomes a JSON array of arrays. Line feeds will be added between
10270
- dimension 1 elements if <parameter>pretty_bool</parameter> is true.
10286
+ dimension- 1 elements if <parameter>pretty_bool</parameter> is true.
10271
10287
</entry>
10272
10288
<entry><literal>array_to_json('{{1,5},{99,100}}'::int[])</literal></entry>
10273
10289
<entry><literal>[[1,5],[99,100]]</literal></entry>
@@ -10277,26 +10293,12 @@ table2-mapping
10277
10293
<literal>row_to_json(record [, pretty_bool])</literal>
10278
10294
</entry>
10279
10295
<entry>
10280
- Returns the row as JSON. Line feeds will be added between level
10281
- 1 elements if <parameter>pretty_bool</parameter> is true.
10296
+ Returns the row as a JSON object . Line feeds will be added between
10297
+ level- 1 elements if <parameter>pretty_bool</parameter> is true.
10282
10298
</entry>
10283
10299
<entry><literal>row_to_json(row(1,'foo'))</literal></entry>
10284
10300
<entry><literal>{"f1":1,"f2":"foo"}</literal></entry>
10285
10301
</row>
10286
- <row>
10287
- <entry>
10288
- <literal>to_json(anyelement)</literal>
10289
- </entry>
10290
- <entry>
10291
- Returns the value as JSON. If the data type is not built in, and there
10292
- is a cast from the type to <type>json</type>, the cast function will be used to
10293
- perform the conversion. Otherwise, for any value other than a number,
10294
- a Boolean, or a null value, the text representation will be used, escaped and
10295
- quoted so that it is legal JSON.
10296
- </entry>
10297
- <entry><literal>to_json('Fred said "Hi."'::text)</literal></entry>
10298
- <entry><literal>"Fred said \"Hi.\""</literal></entry>
10299
- </row>
10300
10302
<row>
10301
10303
<entry>
10302
10304
<literal>json_build_array(VARIADIC "any")</literal>
@@ -10318,7 +10320,7 @@ table2-mapping
10318
10320
names and values.
10319
10321
</entry>
10320
10322
<entry><literal>json_build_object('foo',1,'bar',2)</literal></entry>
10321
- <entry><literal>{"foo" : 1, "bar" : 2}</literal></entry>
10323
+ <entry><literal>{"foo": 1, "bar": 2}</literal></entry>
10322
10324
</row>
10323
10325
<row>
10324
10326
<entry>
@@ -10333,7 +10335,7 @@ table2-mapping
10333
10335
</entry>
10334
10336
<entry><para><literal>json_object('{a, 1, b, "def", c, 3.5}')</></para>
10335
10337
<para><literal>json_object('{{a, 1},{b, "def"},{c, 3.5}}')</></para></entry>
10336
- <entry><literal>{"a" : "1", "b" : "def", "c" : "3.5"}</literal></entry>
10338
+ <entry><literal>{"a": "1", "b": "def", "c": "3.5"}</literal></entry>
10337
10339
</row>
10338
10340
<row>
10339
10341
<entry>
@@ -10344,12 +10346,30 @@ table2-mapping
10344
10346
arrays. In all other respects it is identical to the one-argument form.
10345
10347
</entry>
10346
10348
<entry><literal>json_object('{a, b}', '{1,2}')</literal></entry>
10347
- <entry><literal>{"a" : "1", "b" : "2"}</literal></entry>
10349
+ <entry><literal>{"a": "1", "b": "2"}</literal></entry>
10348
10350
</row>
10349
10351
</tbody>
10350
10352
</tgroup>
10351
10353
</table>
10352
10354
10355
+ <note>
10356
+ <para>
10357
+ <function>array_to_json</> and <function>row_to_json</> have the same
10358
+ behavior as <function>to_json</> except for offering a pretty-printing
10359
+ option. The behavior described for <function>to_json</> likewise applies
10360
+ to each individual value converted by the other JSON creation functions.
10361
+ </para>
10362
+ </note>
10363
+
10364
+ <note>
10365
+ <para>
10366
+ The <xref linkend="hstore"> extension has a cast
10367
+ from <type>hstore</type> to <type>json</type>, so that
10368
+ <type>hstore</type> values converted via the JSON creation functions
10369
+ will be represented as JSON objects, not as primitive string values.
10370
+ </para>
10371
+ </note>
10372
+
10353
10373
<para>
10354
10374
<xref linkend="functions-json-processing-table"> shows the functions that
10355
10375
are available for processing <type>json</type> and <type>jsonb</type> values.
@@ -10479,13 +10499,13 @@ table2-mapping
10479
10499
</entry>
10480
10500
</row>
10481
10501
<row>
10482
- <entry><para><literal>json_each_text(from_json json)</literal>
10483
- </para><para><literal>jsonb_each_text(from_json jsonb)</literal>
10502
+ <entry><para><literal>json_each_text(json)</literal>
10503
+ </para><para><literal>jsonb_each_text(jsonb)</literal>
10484
10504
</para></entry>
10485
10505
<entry><type>setof key text, value text</type></entry>
10486
10506
<entry>
10487
10507
Expands the outermost JSON object into a set of key/value pairs. The
10488
- returned value will be of type <type>text</>.
10508
+ returned values will be of type <type>text</>.
10489
10509
</entry>
10490
10510
<entry><literal>select * from json_each_text('{"a":"foo", "b":"bar"}')</literal></entry>
10491
10511
<entry>
@@ -10504,7 +10524,7 @@ table2-mapping
10504
10524
<entry><para><type>json</type></para><para><type>jsonb</type>
10505
10525
</para></entry>
10506
10526
<entry>
10507
- Returns JSON value pointed to by <parameter >path_elems</parameter >.
10527
+ Returns JSON value pointed to by <replaceable >path_elems</replaceable >.
10508
10528
</entry>
10509
10529
<entry><literal>json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')</literal></entry>
10510
10530
<entry><literal>{"f5":99,"f6":"foo"}</literal></entry>
@@ -10515,7 +10535,8 @@ table2-mapping
10515
10535
</para></entry>
10516
10536
<entry><type>text</type></entry>
10517
10537
<entry>
10518
- Returns JSON value pointed to by <parameter>path_elems</parameter>.
10538
+ Returns JSON value pointed to by <replaceable>path_elems</replaceable>
10539
+ as <type>text</>.
10519
10540
</entry>
10520
10541
<entry><literal>json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')</literal></entry>
10521
10542
<entry><literal>foo</literal></entry>
@@ -10526,7 +10547,7 @@ table2-mapping
10526
10547
</para></entry>
10527
10548
<entry><type>setof text</type></entry>
10528
10549
<entry>
10529
- Returns set of keys in the JSON object. Only the <quote>outer</quote> object will be displayed .
10550
+ Returns set of keys in the outermost JSON object.
10530
10551
</entry>
10531
10552
<entry><literal>json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')</literal></entry>
10532
10553
<entry>
@@ -10544,13 +10565,11 @@ table2-mapping
10544
10565
</para></entry>
10545
10566
<entry><type>anyelement</type></entry>
10546
10567
<entry>
10547
- Expands the object in <replaceable>from_json</replaceable> to a row whose columns match
10548
- the record type defined by base. Conversion will be best
10549
- effort; columns in base with no corresponding key in <replaceable>from_json</replaceable>
10550
- will be left null. When processing <type>json</type>, if a
10551
- column is specified more than once, the last value is used.
10568
+ Expands the object in <replaceable>from_json</replaceable> to a row
10569
+ whose columns match the record type defined by <replaceable>base</>
10570
+ (see note below).
10552
10571
</entry>
10553
- <entry><literal>select * from json_populate_record(null::x , '{"a":1,"b":2}')</literal></entry>
10572
+ <entry><literal>select * from json_populate_record(null::myrowtype , '{"a":1,"b":2}')</literal></entry>
10554
10573
<entry>
10555
10574
<programlisting>
10556
10575
a | b
@@ -10565,14 +10584,12 @@ table2-mapping
10565
10584
</para></entry>
10566
10585
<entry><type>setof anyelement</type></entry>
10567
10586
<entry>
10568
- Expands the outermost set of objects in <replaceable>from_json</replaceable> to a set
10569
- whose columns match the record type defined by base.
10570
- Conversion will be best effort; columns in base with no
10571
- corresponding key in <replaceable>from_json</replaceable> will be left null.
10572
- When processing <type>json</type>, if a column is specified more
10573
- than once, the last value is used.
10587
+ Expands the outermost array of objects
10588
+ in <replaceable>from_json</replaceable> to a set of rows whose
10589
+ columns match the record type defined by <replaceable>base</> (see
10590
+ note below).
10574
10591
</entry>
10575
- <entry><literal>select * from json_populate_recordset(null::x , '[{"a":1,"b":2},{"a":3,"b":4}]')</literal></entry>
10592
+ <entry><literal>select * from json_populate_recordset(null::myrowtype , '[{"a":1,"b":2},{"a":3,"b":4}]')</literal></entry>
10576
10593
<entry>
10577
10594
<programlisting>
10578
10595
a | b
@@ -10627,10 +10644,10 @@ table2-mapping
10627
10644
</para></entry>
10628
10645
<entry><type>text</type></entry>
10629
10646
<entry>
10630
- Returns the type of the outermost JSON value as a text string. The types are
10647
+ Returns the type of the outermost JSON value as a text string.
10648
+ Possible types are
10631
10649
<literal>object</>, <literal>array</>, <literal>string</>, <literal>number</>,
10632
- <literal>boolean</>, and <literal>null</>. (See note below regarding the
10633
- distinction between a JSON <literal>null</> and a SQL NULL.)
10650
+ <literal>boolean</>, and <literal>null</>.
10634
10651
</entry>
10635
10652
<entry><literal>json_typeof('-123.4')</literal></entry>
10636
10653
<entry><literal>number</literal></entry>
@@ -10641,11 +10658,11 @@ table2-mapping
10641
10658
</para></entry>
10642
10659
<entry><type>record</type></entry>
10643
10660
<entry>
10644
- Returns an arbitrary record from a JSON object. As with all functions
10645
- returning <type>record</>, the caller must explicitly define the structure of the record
10646
- when making the call. The input JSON must be an object, not a scalar or an array.
10647
- If <literal >nested_as_text</> is true, the function coerces nested complex elements to text.
10648
- Also, see notes below on columns and types .
10661
+ Builds an arbitrary record from a JSON object (see note below). As
10662
+ with all functions returning <type>record</>, the caller must
10663
+ explicitly define the structure of the record with an <literal>AS</>
10664
+ clause. If <replaceable >nested_as_text</> is true, the function
10665
+ coerces nested complex elements to text .
10649
10666
</entry>
10650
10667
<entry><literal>select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}',true) as x(a int, b text, d text) </literal></entry>
10651
10668
<entry>
@@ -10662,10 +10679,11 @@ table2-mapping
10662
10679
</para></entry>
10663
10680
<entry><type>setof record</type></entry>
10664
10681
<entry>
10665
- Returns an arbitrary set of records from a JSON object. As with
10666
- <function>json_to_record</>, the structure of the record must be explicitly defined when making the
10667
- call. However, with <function>json_to_recordset</> the input JSON must be an array containing
10668
- objects. <literal>nested_as_text</> works as with <function>json_to_record</>.
10682
+ Builds an arbitrary set of records from a JSON array of objects (see
10683
+ note below). As with all functions returning <type>record</>, the
10684
+ caller must explicitly define the structure of the record with
10685
+ an <literal>AS</> clause. <replaceable>nested_as_text</> works as
10686
+ with <function>json_to_record</>.
10669
10687
</entry>
10670
10688
<entry><literal>select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]',true) as x(a int, b text);</literal></entry>
10671
10689
<entry>
@@ -10681,44 +10699,25 @@ table2-mapping
10681
10699
</tgroup>
10682
10700
</table>
10683
10701
10684
- <note>
10685
- <para>
10686
- The <type>json</type> functions and operators can impose stricter
10687
- validity requirements than the JSON types' input functions do. In
10688
- particular, they check much more closely that any use of Unicode
10689
- surrogate pairs to designate characters outside the Unicode Basic
10690
- Multilingual Plane is correct.
10691
- </para>
10692
- </note>
10693
-
10694
10702
<note>
10695
10703
<para>
10696
10704
Many of these functions and operators will convert Unicode escapes in
10697
- the JSON text to the appropriate UTF8 character when the database
10698
- encoding is UTF8. In other encodings the escape sequence must be for an
10699
- ASCII character, and any other code point in a Unicode escape sequence
10700
- will result in an error. In general, it is best to avoid mixing Unicode
10701
- escapes in JSON with a non-UTF8 database encoding, if possible.
10705
+ JSON strings to the appropriate single character. This is a non-issue
10706
+ if the input is type <type>jsonb</>, because the conversion was already
10707
+ done; but for <type>json</> input, this may result in throwing an error,
10708
+ as noted in <xref linkend="datatype-json">.
10702
10709
</para>
10703
10710
</note>
10704
10711
10705
10712
<note>
10706
10713
<para>
10707
- In <function>json_to_record</> and <function>json_to_recordset</>,
10714
+ In <function>json_populate_record</>, <function>json_populate_recordset</>,
10715
+ <function>json_to_record</> and <function>json_to_recordset</>,
10708
10716
type coercion from the JSON is <quote>best effort</> and may not result
10709
- in desired values for some types. JSON elements are matched to
10710
- identical field names in the record definition, and elements which do
10711
- not exist in the JSON will simply be NULL. JSON elements which are not
10712
- defined in the record template will be omitted from the output.
10713
- </para>
10714
- </note>
10715
-
10716
- <note>
10717
- <para>
10718
- The <xref linkend="hstore"> extension has a cast
10719
- from <type>hstore</type> to <type>json</type>, so that
10720
- converted <type>hstore</type> values are represented as JSON objects,
10721
- not as string values.
10717
+ in desired values for some types. JSON keys are matched to
10718
+ identical field names in the target row type, and fields that do
10719
+ not exist in the JSON will simply be NULL. JSON keys that do not
10720
+ appear in the target row type will be omitted from the output.
10722
10721
</para>
10723
10722
</note>
10724
10723
@@ -10739,6 +10738,7 @@ table2-mapping
10739
10738
<function>json_object_agg</function> which aggregates pairs of values
10740
10739
into a JSON object.
10741
10740
</para>
10741
+
10742
10742
</sect1>
10743
10743
10744
10744
<sect1 id="functions-sequence">
0 commit comments