@@ -10245,9 +10245,10 @@ table2-mapping
10245
10245
10246
10246
<para>
10247
10247
<xref linkend="functions-json-creation-table"> shows the functions that are
10248
- available for creating <type>json</type> values.
10249
- (Currently, there are no equivalent functions for <type>jsonb</>, but you
10250
- can cast the result of one of these functions to <type>jsonb</>.)
10248
+ available for creating <type>json</type> and <type>jsonb</type> values.
10249
+ (There are no equivalent functions for <type>jsonb</>, of the <literal>row_to_json</>
10250
+ and <literal>array_to_json</> functions. However, the <literal>to_jsonb</>
10251
+ function supplies much the same functionality as these functions would.)
10251
10252
</para>
10252
10253
10253
10254
<indexterm>
@@ -10268,6 +10269,18 @@ table2-mapping
10268
10269
<indexterm>
10269
10270
<primary>json_object</primary>
10270
10271
</indexterm>
10272
+ <indexterm>
10273
+ <primary>to_jsonb</primary>
10274
+ </indexterm>
10275
+ <indexterm>
10276
+ <primary>jsonb_build_array</primary>
10277
+ </indexterm>
10278
+ <indexterm>
10279
+ <primary>jsonb_build_object</primary>
10280
+ </indexterm>
10281
+ <indexterm>
10282
+ <primary>jsonb_object</primary>
10283
+ </indexterm>
10271
10284
10272
10285
<table id="functions-json-creation-table">
10273
10286
<title>JSON Creation Functions</title>
@@ -10282,17 +10295,18 @@ table2-mapping
10282
10295
</thead>
10283
10296
<tbody>
10284
10297
<row>
10298
+ <entry><para><literal>to_json(anyelement)</literal>
10299
+ </para><para><literal>to_jsonb(anyelement)</literal>
10300
+ </para></entry>
10285
10301
<entry>
10286
- <literal>to_json(anyelement)</literal>
10287
- </entry>
10288
- <entry>
10289
- Returns the value as JSON. Arrays and composites are converted
10302
+ Returns the value as <type>json</> or <type>jsonb</>.
10303
+ Arrays and composites are converted
10290
10304
(recursively) to arrays and objects; otherwise, if there is a cast
10291
10305
from the type to <type>json</type>, the cast function will be used to
10292
- perform the conversion; otherwise, a JSON scalar value is produced.
10306
+ perform the conversion; otherwise, a scalar value is produced.
10293
10307
For any scalar type other than a number, a Boolean, or a null value,
10294
- the text representation will be used, properly quoted and escaped
10295
- so that it is a valid JSON string .
10308
+ the text representation will be used, in such a fashion that it is a
10309
+ valid <type>json</> or <type>jsonb</> value .
10296
10310
</entry>
10297
10311
<entry><literal>to_json('Fred said "Hi."'::text)</literal></entry>
10298
10312
<entry><literal>"Fred said \"Hi.\""</literal></entry>
@@ -10321,9 +10335,9 @@ table2-mapping
10321
10335
<entry><literal>{"f1":1,"f2":"foo"}</literal></entry>
10322
10336
</row>
10323
10337
<row>
10324
- <entry>
10325
- < literal>json_build_array (VARIADIC "any")</literal>
10326
- </entry>
10338
+ <entry><para><literal>json_build_array(VARIADIC "any")</literal>
10339
+ </para><para>< literal>jsonb_build_array (VARIADIC "any")</literal>
10340
+ </para></ entry>
10327
10341
<entry>
10328
10342
Builds a possibly-heterogeneously-typed JSON array out of a variadic
10329
10343
argument list.
@@ -10332,9 +10346,9 @@ table2-mapping
10332
10346
<entry><literal>[1, 2, "3", 4, 5]</literal></entry>
10333
10347
</row>
10334
10348
<row>
10335
- <entry>
10336
- < literal>json_build_object (VARIADIC "any")</literal>
10337
- </entry>
10349
+ <entry><para><literal>json_build_object(VARIADIC "any")</literal>
10350
+ </para><para>< literal>jsonb_build_object (VARIADIC "any")</literal>
10351
+ </para></ entry>
10338
10352
<entry>
10339
10353
Builds a JSON object out of a variadic argument list. By
10340
10354
convention, the argument list consists of alternating
@@ -10344,9 +10358,9 @@ table2-mapping
10344
10358
<entry><literal>{"foo": 1, "bar": 2}</literal></entry>
10345
10359
</row>
10346
10360
<row>
10347
- <entry>
10348
- < literal>json_object (text[])</literal>
10349
- </entry>
10361
+ <entry><para><literal>json_object(text[])</literal>
10362
+ </para><para>< literal>jsonb_object (text[])</literal>
10363
+ </para></ entry>
10350
10364
<entry>
10351
10365
Builds a JSON object out of a text array. The array must have either
10352
10366
exactly one dimension with an even number of members, in which case
@@ -10359,9 +10373,9 @@ table2-mapping
10359
10373
<entry><literal>{"a": "1", "b": "def", "c": "3.5"}</literal></entry>
10360
10374
</row>
10361
10375
<row>
10362
- <entry>
10363
- <literal>json_object(keys text[], values text[])</literal>
10364
- </entry>
10376
+ <entry><para><literal>json_object(keys text[], values text[])</literal>
10377
+ </para><para> <literal>json_object(keys text[], values text[])</literal>
10378
+ </para></ entry>
10365
10379
<entry>
10366
10380
This form of <function>json_object</> takes keys and values pairwise from two separate
10367
10381
arrays. In all other respects it is identical to the one-argument form.
@@ -10780,7 +10794,8 @@ table2-mapping
10780
10794
function <function>json_agg</function> which aggregates record
10781
10795
values as JSON, and the aggregate function
10782
10796
<function>json_object_agg</function> which aggregates pairs of values
10783
- into a JSON object.
10797
+ into a JSON object, and their <type>jsonb</type> equivalents,
10798
+ <function>jsonb_agg</> and <function>jsonb_object_agg</>.
10784
10799
</para>
10785
10800
10786
10801
</sect1>
@@ -12224,6 +12239,22 @@ NULL baz</literallayout>(3 rows)</entry>
12224
12239
<entry>aggregates records as a JSON array of objects</entry>
12225
12240
</row>
12226
12241
12242
+ <row>
12243
+ <entry>
12244
+ <indexterm>
12245
+ <primary>jsonb_agg</primary>
12246
+ </indexterm>
12247
+ <function>jsonb_agg(<replaceable class="parameter">record</replaceable>)</function>
12248
+ </entry>
12249
+ <entry>
12250
+ <type>record</type>
12251
+ </entry>
12252
+ <entry>
12253
+ <type>jsonb</type>
12254
+ </entry>
12255
+ <entry>aggregates records as a JSON array of objects</entry>
12256
+ </row>
12257
+
12227
12258
<row>
12228
12259
<entry>
12229
12260
<indexterm>
@@ -12240,6 +12271,22 @@ NULL baz</literallayout>(3 rows)</entry>
12240
12271
<entry>aggregates name/value pairs as a JSON object</entry>
12241
12272
</row>
12242
12273
12274
+ <row>
12275
+ <entry>
12276
+ <indexterm>
12277
+ <primary>jsonb_object_agg</primary>
12278
+ </indexterm>
12279
+ <function>jsonb_object_agg(<replaceable class="parameter">name</replaceable>, <replaceable class="parameter">value</replaceable>)</function>
12280
+ </entry>
12281
+ <entry>
12282
+ <type>("any", "any")</type>
12283
+ </entry>
12284
+ <entry>
12285
+ <type>jsonb</type>
12286
+ </entry>
12287
+ <entry>aggregates name/value pairs as a JSON object</entry>
12288
+ </row>
12289
+
12243
12290
<row>
12244
12291
<entry>
12245
12292
<indexterm>
@@ -12386,8 +12433,8 @@ SELECT count(*) FROM sometable;
12386
12433
12387
12434
<para>
12388
12435
The aggregate functions <function>array_agg</function>,
12389
- <function>json_agg</function>,
12390
- <function>json_object_agg</function>,
12436
+ <function>json_agg</function>, <function>jsonb_agg</function>,
12437
+ <function>json_object_agg</function>, <function>jsonb_object_agg</function>,
12391
12438
<function>string_agg</function>,
12392
12439
and <function>xmlagg</function>, as well as similar user-defined
12393
12440
aggregate functions, produce meaningfully different result values
0 commit comments