From 7e354ab9fe9e7c3b0a7a40f226c76bd5cf6438d0 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Fri, 12 Dec 2014 15:31:14 -0500 Subject: Add several generator functions for jsonb that exist for json. The functions are: to_jsonb() jsonb_object() jsonb_build_object() jsonb_build_array() jsonb_agg() jsonb_object_agg() Also along the way some better logic is implemented in json_categorize_type() to match that in the newly implemented jsonb_categorize_type(). Andrew Dunstan, reviewed by Pavel Stehule and Alvaro Herrera. --- doc/src/sgml/func.sgml | 97 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 25 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index da138e1fee7..ef69b94cf03 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -10245,9 +10245,10 @@ table2-mapping shows the functions that are - available for creating json values. - (Currently, there are no equivalent functions for jsonb, but you - can cast the result of one of these functions to jsonb.) + available for creating json and jsonb values. + (There are no equivalent functions for jsonb, of the row_to_json + and array_to_json functions. However, the to_jsonb + function supplies much the same functionality as these functions would.) @@ -10268,6 +10269,18 @@ table2-mapping json_object + + to_jsonb + + + jsonb_build_array + + + jsonb_build_object + + + jsonb_object + JSON Creation Functions @@ -10282,17 +10295,18 @@ table2-mapping + to_json(anyelement) + to_jsonb(anyelement) + - to_json(anyelement) - - - Returns the value as JSON. Arrays and composites are converted + Returns the value as json or jsonb. + Arrays and composites are converted (recursively) to arrays and objects; otherwise, if there is a cast from the type to json, the cast function will be used to - perform the conversion; otherwise, a JSON scalar value is produced. + 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, properly quoted and escaped - so that it is a valid JSON string. + the text representation will be used, in such a fashion that it is a + valid json or jsonb value. to_json('Fred said "Hi."'::text) "Fred said \"Hi.\"" @@ -10321,9 +10335,9 @@ table2-mapping {"f1":1,"f2":"foo"} - - json_build_array(VARIADIC "any") - + json_build_array(VARIADIC "any") + jsonb_build_array(VARIADIC "any") + Builds a possibly-heterogeneously-typed JSON array out of a variadic argument list. @@ -10332,9 +10346,9 @@ table2-mapping [1, 2, "3", 4, 5] - - json_build_object(VARIADIC "any") - + json_build_object(VARIADIC "any") + jsonb_build_object(VARIADIC "any") + Builds a JSON object out of a variadic argument list. By convention, the argument list consists of alternating @@ -10344,9 +10358,9 @@ table2-mapping {"foo": 1, "bar": 2} - - json_object(text[]) - + json_object(text[]) + jsonb_object(text[]) + 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 @@ -10359,9 +10373,9 @@ table2-mapping {"a": "1", "b": "def", "c": "3.5"} - - json_object(keys text[], values text[]) - + json_object(keys text[], values text[]) + json_object(keys text[], values text[]) + This form of json_object takes keys and values pairwise from two separate arrays. In all other respects it is identical to the one-argument form. @@ -10780,7 +10794,8 @@ table2-mapping function json_agg which aggregates record values as JSON, and the aggregate function json_object_agg which aggregates pairs of values - into a JSON object. + into a JSON object, and their jsonb equivalents, + jsonb_agg and jsonb_object_agg. @@ -12224,6 +12239,22 @@ NULL baz(3 rows) aggregates records as a JSON array of objects + + + + jsonb_agg + + jsonb_agg(record) + + + record + + + jsonb + + aggregates records as a JSON array of objects + + @@ -12240,6 +12271,22 @@ NULL baz(3 rows) aggregates name/value pairs as a JSON object + + + + jsonb_object_agg + + jsonb_object_agg(name, value) + + + ("any", "any") + + + jsonb + + aggregates name/value pairs as a JSON object + + @@ -12386,8 +12433,8 @@ SELECT count(*) FROM sometable; The aggregate functions array_agg, - json_agg, - json_object_agg, + json_agg, jsonb_agg, + json_object_agg, jsonb_object_agg, string_agg, and xmlagg, as well as similar user-defined aggregate functions, produce meaningfully different result values -- cgit v1.2.3