pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14178: output of jsonb_object and json_object doesn't match textually
9+ messages / 4 participants
[nested] [flat]

* BUG #14178: output of jsonb_object and json_object doesn't match textually
@ 2016-06-07 01:42 ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: ap@zip.com.au @ 2016-06-07 01:42 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14178
Logged by:          Andrew P
Email address:      ap@zip.com.au
PostgreSQL version: 9.5.1
Operating system:   Linux, Debian, Jessie
Description:        

Hi,

When presenting JSON output as TEXT, json_object seems to be the odd muppet
out:

field=# select json_object(ARRAY['moo', 'woof'], ARRAY['cow',
'dog'])::text;
           json_object           
---------------------------------
 {"moo" : "cow", "woof" : "dog"}
(1 row)

Time: 0.428 ms
field=# select jsonb_object(ARRAY['moo', 'woof'], ARRAY['cow',
'dog'])::text;
         jsonb_object          
-------------------------------
 {"moo": "cow", "woof": "dog"}
(1 row)

Time: 0.306 ms

I tried a few functions and all appear to be leaving no space before the
colon. Even to_json:

field=# select to_json('{"moo":        "cow"}'::jsonb);
    to_json     
----------------
 {"moo": "cow"}
(1 row)

Time: 0.400 ms

And cast:

field=# select cast('{"moo":        "cow"}'::jsonb as json);
      json      
----------------
 {"moo": "cow"}
(1 row)

Time: 0.398 ms

I think json_object should provide similar output. Consistency is nice 
aesthetically and as being in line with the principle of least surprise.
Its lack may lead to possible unpleasant results if anyone tries to deal
with a JSON structure as TEXT (for whatever reason).

Andrew



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
@ 2016-06-07 02:40 ` Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Michael Paquier @ 2016-06-07 02:40 UTC (permalink / raw)
  To: ap@zip.com.au; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 10:42 AM,  <ap@zip.com.au> wrote:
> I think json_object should provide similar output. Consistency is nice
> aesthetically and as being in line with the principle of least surprise.
> Its lack may lead to possible unpleasant results if anyone tries to deal
> with a JSON structure as TEXT (for whatever reason).

That's a debatable point, and as any output is legal json it does not
actually hurt one way or another. However, as json_object has been
introduced at the same time as jsonb, I'd tend as well to think that
consistency is a good idea if possible, see the patch attached.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

json-object-format.patch (text/x-diff, 1.6K) download diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index f704418..c8b3970 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -2311,7 +2311,7 @@ json_object(PG_FUNCTION_ARGS) if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); - appendStringInfoString(&result, " : "); + appendStringInfoString(&result, ": "); pfree(v); if (in_nulls[i * 2 + 1]) appendStringInfoString(&result, "null"); diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index efcdc41..f8eff07 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -1526,16 +1526,16 @@ SELECT json_object('{}', '{}'); -- one dimension SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- same but with two dimensions SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- odd number error
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
@ 2016-06-07 06:48   ` Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2016-06-07 06:48 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 2:40 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Tue, Jun 7, 2016 at 10:42 AM,  <ap@zip.com.au> wrote:
>> I think json_object should provide similar output. Consistency is nice
>> aesthetically and as being in line with the principle of least surprise.
>> Its lack may lead to possible unpleasant results if anyone tries to deal
>> with a JSON structure as TEXT (for whatever reason).
>
> That's a debatable point, and as any output is legal json it does not
> actually hurt one way or another. However, as json_object has been
> introduced at the same time as jsonb, I'd tend as well to think that
> consistency is a good idea if possible, see the patch attached.

(It's a shame that json_build_object and json_object_agg also don't
agree on where to put whitespace...)

-- 
Thomas Munro
http://www.enterprisedb.com


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
@ 2016-06-07 06:54     ` Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 07:55       ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Michael Paquier @ 2016-06-07 06:54 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@enterprisedb.com>; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 3:48 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
> On Tue, Jun 7, 2016 at 2:40 PM, Michael Paquier
> <michael.paquier@gmail.com> wrote:
>> On Tue, Jun 7, 2016 at 10:42 AM,  <ap@zip.com.au> wrote:
>>> I think json_object should provide similar output. Consistency is nice
>>> aesthetically and as being in line with the principle of least surprise.
>>> Its lack may lead to possible unpleasant results if anyone tries to deal
>>> with a JSON structure as TEXT (for whatever reason).
>>
>> That's a debatable point, and as any output is legal json it does not
>> actually hurt one way or another. However, as json_object has been
>> introduced at the same time as jsonb, I'd tend as well to think that
>> consistency is a good idea if possible, see the patch attached.
>
> (It's a shame that json_build_object and json_object_agg also don't
> agree on where to put whitespace...)

Right, missed that, as well as json_object_two_arg.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

json-object-format-v2.patch (text/x-diff, 7.0K) download diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index f704418..fb85cd7 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -2044,7 +2044,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS) datum_to_json(arg, false, state->str, state->key_category, state->key_output_func, true); - appendStringInfoString(state->str, " : "); + appendStringInfoString(state->str, ": "); if (PG_ARGISNULL(2)) arg = (Datum) 0; @@ -2152,7 +2152,7 @@ json_build_object(PG_FUNCTION_ARGS) add_json(arg, false, result, val_type, true); - appendStringInfoString(result, " : "); + appendStringInfoString(result, ": "); /* process value */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1); @@ -2311,7 +2311,7 @@ json_object(PG_FUNCTION_ARGS) if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); - appendStringInfoString(&result, " : "); + appendStringInfoString(&result, ": "); pfree(v); if (in_nulls[i * 2 + 1]) appendStringInfoString(&result, "null"); @@ -2395,7 +2395,7 @@ json_object_two_arg(PG_FUNCTION_ARGS) if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); - appendStringInfoString(&result, " : "); + appendStringInfoString(&result, ": "); pfree(v); if (val_nulls[i]) appendStringInfoString(&result, "null"); diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index efcdc41..0e66ce6 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -1445,18 +1445,18 @@ SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": (1 row) SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); - json_build_object ----------------------------------------------------------------------------- - {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}} + json_build_object +----------------------------------------------------------------------- + {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1,2,3]}} (1 row) SELECT json_build_object( 'a', json_build_object('b',false,'c',99), 'd', json_build_object('e',array[9,8,7]::int[], 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r))); - json_build_object -------------------------------------------------------------------------------------------------- - {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}} + json_build_object +------------------------------------------------------------------------------------------- + {"a": {"b": false, "c": 99}, "d": {"e": [9,8,7], "f": {"relkind":"r","name":"pg_class"}}} (1 row) -- empty objects/arrays @@ -1476,7 +1476,7 @@ SELECT json_build_object(); SELECT json_build_object(1,2); json_build_object ------------------- - {"1" : 2} + {"1": 2} (1 row) -- keys must be scalar and not null @@ -1495,15 +1495,15 @@ INSERT INTO foo VALUES (847002,'t16','GE1043'); INSERT INTO foo VALUES (847003,'sub-alpha','GESS90'); SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type))) FROM foo; - json_build_object -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }} + json_build_object +--------------------------------------------------------------------------------------------------------------------------------------------------------------- + {"turbines": { "847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"} }} (1 row) SELECT json_object_agg(name, type) FROM foo; - json_object_agg ----------------------------------------------------------------- - { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" } + json_object_agg +------------------------------------------------------------- + { "t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90" } (1 row) INSERT INTO foo VALUES (999999, NULL, 'bar'); @@ -1526,16 +1526,16 @@ SELECT json_object('{}', '{}'); -- one dimension SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- same but with two dimensions SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- odd number error @@ -1552,9 +1552,9 @@ SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}'); ERROR: wrong number of array subscripts --two argument form of json_object select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}'); - json_object ------------------------------------------------------- - {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"} + json_object +-------------------------------------------------- + {"a": "1", "b": "2", "c": "3", "d e f": "a b c"} (1 row) -- too many dimensions @@ -1570,9 +1570,9 @@ select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}'); ERROR: null value not allowed for object key -- empty key is allowed select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}'); - json_object ------------------------------------------------------ - {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"} + json_object +------------------------------------------------- + {"a": "1", "b": "2", "": "3", "d e f": "a b c"} (1 row) -- json_to_record and json_to_recordset
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
@ 2016-06-07 07:55       ` Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 11:58         ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2016-06-07 07:55 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 6:54 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Tue, Jun 7, 2016 at 3:48 PM, Thomas Munro
> <thomas.munro@enterprisedb.com> wrote:
>> On Tue, Jun 7, 2016 at 2:40 PM, Michael Paquier
>> <michael.paquier@gmail.com> wrote:
>>> On Tue, Jun 7, 2016 at 10:42 AM,  <ap@zip.com.au> wrote:
>>>> I think json_object should provide similar output. Consistency is nice
>>>> aesthetically and as being in line with the principle of least surprise.
>>>> Its lack may lead to possible unpleasant results if anyone tries to deal
>>>> with a JSON structure as TEXT (for whatever reason).
>>>
>>> That's a debatable point, and as any output is legal json it does not
>>> actually hurt one way or another. However, as json_object has been
>>> introduced at the same time as jsonb, I'd tend as well to think that
>>> consistency is a good idea if possible, see the patch attached.
>>
>> (It's a shame that json_build_object and json_object_agg also don't
>> agree on where to put whitespace...)
>
> Right, missed that, as well as json_object_two_arg.

One more difference is that json_object_agg uses "{ " and " }" while
the other functions use "{" and "}".

-- 
Thomas Munro
http://www.enterprisedb.com


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 07:55       ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
@ 2016-06-07 11:58         ` Michael Paquier <michael.paquier@gmail.com>
  2016-06-08 02:59           ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Michael Paquier @ 2016-06-07 11:58 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@enterprisedb.com>; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 4:55 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
> On Tue, Jun 7, 2016 at 6:54 PM, Michael Paquier
> <michael.paquier@gmail.com> wrote:
>> On Tue, Jun 7, 2016 at 3:48 PM, Thomas Munro
>> <thomas.munro@enterprisedb.com> wrote:
>>> On Tue, Jun 7, 2016 at 2:40 PM, Michael Paquier
>>> <michael.paquier@gmail.com> wrote:
>>>> On Tue, Jun 7, 2016 at 10:42 AM,  <ap@zip.com.au> wrote:
>>>>> I think json_object should provide similar output. Consistency is nice
>>>>> aesthetically and as being in line with the principle of least surprise.
>>>>> Its lack may lead to possible unpleasant results if anyone tries to deal
>>>>> with a JSON structure as TEXT (for whatever reason).
>>>>
>>>> That's a debatable point, and as any output is legal json it does not
>>>> actually hurt one way or another. However, as json_object has been
>>>> introduced at the same time as jsonb, I'd tend as well to think that
>>>> consistency is a good idea if possible, see the patch attached.
>>>
>>> (It's a shame that json_build_object and json_object_agg also don't
>>> agree on where to put whitespace...)
>>
>> Right, missed that, as well as json_object_two_arg.
>
> One more difference is that json_object_agg uses "{ " and " }" while
> the other functions use "{" and "}".

OK, that would make the entries generated a bit more compact.. Are
there other opinions on the matter?
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 07:55       ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 11:58         ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
@ 2016-06-08 02:59           ` Michael Paquier <michael.paquier@gmail.com>
  2016-06-08 10:32             ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually AP <ap@zip.com.au>
  0 siblings, 1 reply; 9+ messages in thread

From: Michael Paquier @ 2016-06-08 02:59 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@enterprisedb.com>; +Cc: pgsql-bugs

On Tue, Jun 7, 2016 at 8:58 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> OK, that would make the entries generated a bit more compact.. Are
> there other opinions on the matter?

I have noticed as well that array elements separated by a comma use a
space after the comma with jsonb, and not with json. Still, doing
improvements in this area would be useful if a user is interested in
doing direct comparison of jsonb with json after casting them into
text to save some parsing cost. Still, an area where things are not
under controll with json is the key ordering within the same nest
level, which is on the contrary pre-defined in jsonb by the unicity of
the key names, and that's not the case of json. Take this example:

=# select row_to_json(r)::json from ( select relkind, oid::regclass as
name from pg_class where relname = 'pg_class') r;
            row_to_json
------------------------------------
 {"relkind":"r", "name":"pg_class"}
(1 row)
=# select row_to_json(r)::jsonb from (select relkind, oid::regclass as
name from pg_class where relname = 'pg_class') r;
             row_to_json
--------------------------------------
 {"name": "pg_class", "relkind": "r"}
(1 row)
This kills any hope of comparison.

So after thinking more on the matter, I'd rather give up on such
patches and recommend doing ::jsonb::text to be able to do equality
comparisons. If you want to get output similar to what jsonb is doing,
a simple cast with ::jsonb looks rather the saner way to go. This
induces more parsing cost, but that's the cost to pay...
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 07:55       ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 11:58         ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-08 02:59           ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
@ 2016-06-08 10:32             ` AP <ap@zip.com.au>
  2016-06-10 11:06               ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually AP <ap@zip.com.au>
  0 siblings, 1 reply; 9+ messages in thread

From: AP @ 2016-06-08 10:32 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Thomas Munro <thomas.munro@enterprisedb.com>; pgsql-bugs

On Wed, Jun 08, 2016 at 11:59:10AM +0900, Michael Paquier wrote:
> text to save some parsing cost. Still, an area where things are not
> under controll with json is the key ordering within the same nest
> level, which is on the contrary pre-defined in jsonb by the unicity of
> the key names, and that's not the case of json. Take this example:
> 
> =# select row_to_json(r)::json from ( select relkind, oid::regclass as
> name from pg_class where relname = 'pg_class') r;
>             row_to_json
> ------------------------------------
>  {"relkind":"r", "name":"pg_class"}
> (1 row)
> =# select row_to_json(r)::jsonb from (select relkind, oid::regclass as
> name from pg_class where relname = 'pg_class') r;
>              row_to_json
> --------------------------------------
>  {"name": "pg_class", "relkind": "r"}
> (1 row)
> This kills any hope of comparison.
> 
> So after thinking more on the matter, I'd rather give up on such
> patches and recommend doing ::jsonb::text to be able to do equality
> comparisons. If you want to get output similar to what jsonb is doing,
> a simple cast with ::jsonb looks rather the saner way to go. This
> induces more parsing cost, but that's the cost to pay...

I think two things are being somewhat conflated:

1. the look of our JSON structures - we should be consistent here
2. the ordering of our JSON structures internally - a related (for the above
   use case issue) but, ultimately, seperate issue.

1 is easy to solve. JSON is JSON is JSON whether it's in a JSON data-type or
JSONB and so no matter which it should look the same.

The types JSON and JSONB, to my knowledge, are different in terms of
internal (wrt postgres) representation and what they promise:

"Because the json type stores an exact copy of the input text, it will
preserve semantically-insignificant white space between tokens, as well as
the order of keys within JSON objects. Also, if a JSON object within the
value contains the same key more than once, all the key/value pairs are
kept. (The processing functions consider the last value as the operative
one.) By contrast, jsonb does not preserve white space, does not preserve
the order of object keys, and does not keep duplicate object keys. If
duplicate keys are specified in the input, only the last value is kept."
- https://www.postgresql.org/docs/current/static/datatype-json.html

As you can see, the key ordering is already covered. If you care, stick to JSON.
If you don't, fly JSONB airlines.

I think it's still worth doing #1 above and I'm willing to do the patch
myself (it didn't occur to me how simple it was and I've been kicking myself
at losing the opportunity to contribute :( :) ) if need be and if it'll fly.

Andrew


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: BUG #14178: output of jsonb_object and json_object doesn't match textually
  2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
  2016-06-07 02:40 ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 06:48   ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 06:54     ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-07 07:55       ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Thomas Munro <thomas.munro@enterprisedb.com>
  2016-06-07 11:58         ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-08 02:59           ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually Michael Paquier <michael.paquier@gmail.com>
  2016-06-08 10:32             ` Re: BUG #14178: output of jsonb_object and json_object doesn't match textually AP <ap@zip.com.au>
@ 2016-06-10 11:06               ` AP <ap@zip.com.au>
  0 siblings, 0 replies; 9+ messages in thread

From: AP @ 2016-06-10 11:06 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Thomas Munro <thomas.munro@enterprisedb.com>; pgsql-bugs

On Wed, Jun 08, 2016 at 08:32:41PM +1000, AP wrote:
> > So after thinking more on the matter, I'd rather give up on such
> > patches and recommend doing ::jsonb::text to be able to do equality
> > comparisons. If you want to get output similar to what jsonb is doing,
> > a simple cast with ::jsonb looks rather the saner way to go. This
> > induces more parsing cost, but that's the cost to pay...
> 
> I think two things are being somewhat conflated:
> 
> 1. the look of our JSON structures - we should be consistent here
> 2. the ordering of our JSON structures internally - a related (for the above
>    use case issue) but, ultimately, seperate issue.
> 
> 1 is easy to solve. JSON is JSON is JSON whether it's in a JSON data-type or
> JSONB and so no matter which it should look the same.
(docs snipped)
> As you can see, the key ordering is already covered. If you care, stick to JSON.
> If you don't, fly JSONB airlines.
> 
> I think it's still worth doing #1 above and I'm willing to do the patch
> myself (it didn't occur to me how simple it was and I've been kicking myself
> at losing the opportunity to contribute :( :) ) if need be and if it'll fly.

Well I got bored and did it anyway. I checked all the functions in the doc page
and modified accordingly. My patch is, obviously, a superset of Michael's. :)

Interesting tidbit wrt this bit in the docs for JSON:

"Because the json type stores an exact copy of the input text, it will
preserve semantically-insignificant white space between tokens, as well
as the order of keys within JSON objects."
- https://www.postgresql.org/docs/current/static/datatype-json.html

json_strip_nulls doesn't appear to adhere to that:

moo=# select json_strip_nulls('{ "a":"b", "c":["a",2,-5,null], "d":{"e":"f", "true": false, "false": true, "val": null}}');
                           json_strip_nulls                            
-----------------------------------------------------------------------
 {"a":"b","c":["a",2,-5,null],"d":{"e":"f","true":false,"false":true}}
(1 row)

Not sure if this should be fixed in-code or a note be made in the docs
of the exceptions.

Anyhow, patch attached.

Andrew

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

json-formatting.patch (text/x-diff, 31.1K) download diff -aur postgresql-9.5-9.5.3.orig/src/backend/utils/adt/json.c postgresql-9.5-9.5.3/src/backend/utils/adt/json.c --- postgresql-9.5-9.5.3.orig/src/backend/utils/adt/json.c 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/backend/utils/adt/json.c 2016-06-10 09:56:56.681634320 +1000 @@ -442,7 +442,7 @@ parse_object_field(JsonLexContext *lex, JsonSemAction *sem) { /* - * An object field is "fieldname" : value where value can be a scalar, + * An object field is "fieldname": value where value can be a scalar, * object or array. Note: in user-facing docs and error messages, we * generally call a field name a "key". */ @@ -1594,7 +1594,7 @@ Assert(dim < ndims); - sep = use_line_feeds ? ",\n " : ","; + sep = use_line_feeds ? ",\n " : ", "; appendStringInfoChar(result, '['); @@ -1686,7 +1686,7 @@ bool needsep = false; const char *sep; - sep = use_line_feeds ? ",\n " : ","; + sep = use_line_feeds ? ",\n " : ", "; td = DatumGetHeapTupleHeader(composite); @@ -1719,7 +1719,7 @@ attname = NameStr(tupdesc->attrs[i]->attname); escape_json(result, attname); - appendStringInfoChar(result, ':'); + appendStringInfoString(result, ": "); val = heap_getattr(tuple, i + 1, tupdesc, &isnull); @@ -2018,7 +2018,7 @@ json_categorize_type(arg_type, &state->val_category, &state->val_output_func); - appendStringInfoString(state->str, "{ "); + appendStringInfoString(state->str, "{"); } else { @@ -2044,7 +2044,7 @@ datum_to_json(arg, false, state->str, state->key_category, state->key_output_func, true); - appendStringInfoString(state->str, " : "); + appendStringInfoString(state->str, ": "); if (PG_ARGISNULL(2)) arg = (Datum) 0; @@ -2075,7 +2075,7 @@ PG_RETURN_NULL(); /* Else return state with appropriate object terminator added */ - PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }")); + PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "}")); } /* @@ -2152,7 +2152,7 @@ add_json(arg, false, result, val_type, true); - appendStringInfoString(result, " : "); + appendStringInfoString(result, ": "); /* process value */ val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1); @@ -2311,7 +2311,7 @@ if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); - appendStringInfoString(&result, " : "); + appendStringInfoString(&result, ": "); pfree(v); if (in_nulls[i * 2 + 1]) appendStringInfoString(&result, "null"); @@ -2395,7 +2395,7 @@ if (i > 0) appendStringInfoString(&result, ", "); escape_json(&result, v); - appendStringInfoString(&result, " : "); + appendStringInfoString(&result, ": "); pfree(v); if (val_nulls[i]) appendStringInfoString(&result, "null"); diff -aur postgresql-9.5-9.5.3.orig/src/backend/utils/adt/jsonfuncs.c postgresql-9.5-9.5.3/src/backend/utils/adt/jsonfuncs.c --- postgresql-9.5-9.5.3.orig/src/backend/utils/adt/jsonfuncs.c 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/backend/utils/adt/jsonfuncs.c 2016-06-10 10:12:43.720160113 +1000 @@ -3147,7 +3147,7 @@ } if (_state->strval->data[_state->strval->len - 1] != '{') - appendStringInfoCharMacro(_state->strval, ','); + appendStringInfoString(_state->strval, ", "); /* * Unfortunately we don't have the quoted and escaped string any more, so @@ -3155,7 +3155,7 @@ */ escape_json(_state->strval, fname); - appendStringInfoCharMacro(_state->strval, ':'); + appendStringInfoString(_state->strval, ": "); } static void @@ -3164,7 +3164,7 @@ StripnullState *_state = (StripnullState *) state; if (_state->strval->data[_state->strval->len - 1] != '[') - appendStringInfoCharMacro(_state->strval, ','); + appendStringInfoString(_state->strval, ", "); } static void diff -aur postgresql-9.5-9.5.3.orig/src/test/regress/expected/copy2.out postgresql-9.5-9.5.3/src/test/regress/expected/copy2.out --- postgresql-9.5-9.5.3.orig/src/test/regress/expected/copy2.out 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/test/regress/expected/copy2.out 2016-06-10 11:16:39.474352171 +1000 @@ -446,12 +446,12 @@ "check_con_tbl_check" CHECK (check_con_function(check_con_tbl.*)) copy check_con_tbl from stdin; -NOTICE: input = {"f1":1} +NOTICE: input = {"f1": 1} CONTEXT: COPY check_con_tbl, line 1: "1" -NOTICE: input = {"f1":null} +NOTICE: input = {"f1": null} CONTEXT: COPY check_con_tbl, line 2: "\N" copy check_con_tbl from stdin; -NOTICE: input = {"f1":0} +NOTICE: input = {"f1": 0} CONTEXT: COPY check_con_tbl, line 1: "0" ERROR: new row for relation "check_con_tbl" violates check constraint "check_con_tbl_check" DETAIL: Failing row contains (0). diff -aur postgresql-9.5-9.5.3.orig/src/test/regress/expected/json.out postgresql-9.5-9.5.3/src/test/regress/expected/json.out --- postgresql-9.5-9.5.3.orig/src/test/regress/expected/json.out 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/test/regress/expected/json.out 2016-06-10 11:10:55.089897082 +1000 @@ -281,17 +281,17 @@ (1 row) SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q; - array_to_json ---------------------------------------------- - [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}] + array_to_json +-------------------------------------------------------- + [{"b": 1, "c": 2}, {"b": 2, "c": 4}, {"b": 3, "c": 6}] (1 row) SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q; - array_to_json ------------------ - [{"b":1,"c":2},+ - {"b":2,"c":4},+ - {"b":3,"c":6}] + array_to_json +-------------------- + [{"b": 1, "c": 2},+ + {"b": 2, "c": 4},+ + {"b": 3, "c": 6}] (1 row) SELECT array_to_json(array_agg(q),false) @@ -300,28 +300,28 @@ ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x, generate_series(4,5) y) q; - array_to_json -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}] + array_to_json +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + [{"b": "a1", "c": 4, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a1", "c": 5, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 4, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 5, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}] (1 row) SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x; - array_to_json ----------------- - [5,6,7,8,9,10] + array_to_json +--------------------- + [5, 6, 7, 8, 9, 10] (1 row) SELECT array_to_json('{{1,5},{99,100}}'::int[]); - array_to_json ------------------- - [[1,5],[99,100]] + array_to_json +--------------------- + [[1, 5], [99, 100]] (1 row) -- row_to_json SELECT row_to_json(row(1,'foo')); - row_to_json ---------------------- - {"f1":1,"f2":"foo"} + row_to_json +------------------------ + {"f1": 1, "f2": "foo"} (1 row) SELECT row_to_json(q) @@ -331,12 +331,12 @@ ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x, generate_series(4,5) y) q; - row_to_json --------------------------------------------------------------------- - {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]} - {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]} - {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]} - {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]} + row_to_json +------------------------------------------------------------------------------------ + {"b": "a1", "c": 4, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]} + {"b": "a1", "c": 5, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]} + {"b": "a2", "c": 4, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]} + {"b": "a2", "c": 5, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]} (4 rows) SELECT row_to_json(q,true) @@ -346,20 +346,20 @@ ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x, generate_series(4,5) y) q; - row_to_json ------------------------------------------------------ - {"b":"a1", + - "c":4, + - "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]} - {"b":"a1", + - "c":5, + - "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]} - {"b":"a2", + - "c":4, + - "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]} - {"b":"a2", + - "c":5, + - "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]} + row_to_json +----------------------------------------------------------------- + {"b": "a1", + + "c": 4, + + "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]} + {"b": "a1", + + "c": 5, + + "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]} + {"b": "a2", + + "c": 4, + + "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]} + {"b": "a2", + + "c": 5, + + "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]} (4 rows) CREATE TEMP TABLE rows AS @@ -367,20 +367,20 @@ FROM generate_series(1,3) AS x; SELECT row_to_json(q,true) FROM rows q; - row_to_json --------------- - {"x":1, + - "y":"txt1"} - {"x":2, + - "y":"txt2"} - {"x":3, + - "y":"txt3"} + row_to_json +--------------- + {"x": 1, + + "y": "txt1"} + {"x": 2, + + "y": "txt2"} + {"x": 3, + + "y": "txt3"} (3 rows) SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false); - row_to_json ------------------------ - {"f1":[5,6,7,8,9,10]} + row_to_json +----------------------------- + {"f1": [5, 6, 7, 8, 9, 10]} (1 row) -- to_json, timestamps @@ -455,61 +455,61 @@ ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x, generate_series(4,5) y) q; - json_agg ------------------------------------------------------------------------ - [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, + - {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, + - {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, + - {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}] + json_agg +--------------------------------------------------------------------------------------- + [{"b": "a1", "c": 4, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, + + {"b": "a1", "c": 5, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}, + + {"b": "a2", "c": 4, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, + + {"b": "a2", "c": 5, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}] (1 row) SELECT json_agg(q ORDER BY x, y) FROM rows q; - json_agg ------------------------ - [{"x":1,"y":"txt1"}, + - {"x":2,"y":"txt2"}, + - {"x":3,"y":"txt3"}] + json_agg +-------------------------- + [{"x": 1, "y": "txt1"}, + + {"x": 2, "y": "txt2"}, + + {"x": 3, "y": "txt3"}] (1 row) UPDATE rows SET x = NULL WHERE x = 1; SELECT json_agg(q ORDER BY x NULLS FIRST, y) FROM rows q; - json_agg --------------------------- - [{"x":null,"y":"txt1"}, + - {"x":2,"y":"txt2"}, + - {"x":3,"y":"txt3"}] + json_agg +----------------------------- + [{"x": null, "y": "txt1"}, + + {"x": 2, "y": "txt2"}, + + {"x": 3, "y": "txt3"}] (1 row) -- non-numeric output SELECT row_to_json(q) FROM (SELECT 'NaN'::float8 AS "float8field") q; - row_to_json ------------------------ - {"float8field":"NaN"} + row_to_json +------------------------ + {"float8field": "NaN"} (1 row) SELECT row_to_json(q) FROM (SELECT 'Infinity'::float8 AS "float8field") q; - row_to_json ----------------------------- - {"float8field":"Infinity"} + row_to_json +----------------------------- + {"float8field": "Infinity"} (1 row) SELECT row_to_json(q) FROM (SELECT '-Infinity'::float8 AS "float8field") q; - row_to_json ------------------------------ - {"float8field":"-Infinity"} + row_to_json +------------------------------ + {"float8field": "-Infinity"} (1 row) -- json input SELECT row_to_json(q) FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q; - row_to_json ------------------------------------------------------------------- - {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}} + row_to_json +------------------------------------------------------------------- + {"jsonfield": {"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}} (1 row) -- json extraction functions @@ -1445,18 +1445,18 @@ (1 row) SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); - json_build_object ----------------------------------------------------------------------------- - {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}} + json_build_object +----------------------------------------------------------------------- + {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1,2,3]}} (1 row) SELECT json_build_object( 'a', json_build_object('b',false,'c',99), 'd', json_build_object('e',array[9,8,7]::int[], 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r))); - json_build_object -------------------------------------------------------------------------------------------------- - {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}} + json_build_object +------------------------------------------------------------------------------------------------ + {"a": {"b": false, "c": 99}, "d": {"e": [9, 8, 7], "f": {"relkind": "r", "name": "pg_class"}}} (1 row) -- empty objects/arrays @@ -1476,7 +1476,7 @@ SELECT json_build_object(1,2); json_build_object ------------------- - {"1" : 2} + {"1": 2} (1 row) -- keys must be scalar and not null @@ -1495,15 +1495,15 @@ INSERT INTO foo VALUES (847003,'sub-alpha','GESS90'); SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type))) FROM foo; - json_build_object -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }} + json_build_object +------------------------------------------------------------------------------------------------------------------------------------------------------------- + {"turbines": {"847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"}}} (1 row) SELECT json_object_agg(name, type) FROM foo; - json_object_agg ----------------------------------------------------------------- - { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" } + json_object_agg +----------------------------------------------------------- + {"t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90"} (1 row) INSERT INTO foo VALUES (999999, NULL, 'bar'); @@ -1526,16 +1526,16 @@ -- one dimension SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- same but with two dimensions SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); - json_object -------------------------------------------------------- - {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"} + json_object +--------------------------------------------------- + {"a": "1", "b": "2", "3": null, "d e f": "a b c"} (1 row) -- odd number error @@ -1552,9 +1552,9 @@ ERROR: wrong number of array subscripts --two argument form of json_object select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}'); - json_object ------------------------------------------------------- - {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"} + json_object +-------------------------------------------------- + {"a": "1", "b": "2", "c": "3", "d e f": "a b c"} (1 row) -- too many dimensions @@ -1570,9 +1570,9 @@ ERROR: null value not allowed for object key -- empty key is allowed select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}'); - json_object ------------------------------------------------------ - {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"} + json_object +------------------------------------------------- + {"a": "1", "b": "2", "": "3", "d e f": "a b c"} (1 row) -- json_to_record and json_to_recordset @@ -1641,27 +1641,27 @@ (1 row) select json_strip_nulls('[1,2,null,3,4]'); - json_strip_nulls ------------------- - [1,2,null,3,4] + json_strip_nulls +-------------------- + [1, 2, null, 3, 4] (1 row) select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}'); - json_strip_nulls ------------------------------------- - {"a":1,"c":[2,null,3],"d":{"e":4}} + json_strip_nulls +-------------------------------------------- + {"a": 1, "c": [2, null, 3], "d": {"e": 4}} (1 row) select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]'); - json_strip_nulls ---------------------- - [1,{"a":1,"c":2},3] + json_strip_nulls +-------------------------- + [1, {"a": 1, "c": 2}, 3] (1 row) -- an empty object is not null and should not be stripped select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }'); - json_strip_nulls ------------------- - {"a":{},"d":{}} + json_strip_nulls +-------------------- + {"a": {}, "d": {}} (1 row) diff -aur postgresql-9.5-9.5.3.orig/src/test/regress/expected/jsonb.out postgresql-9.5-9.5.3/src/test/regress/expected/jsonb.out --- postgresql-9.5-9.5.3.orig/src/test/regress/expected/jsonb.out 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/test/regress/expected/jsonb.out 2016-06-10 11:12:16.320891903 +1000 @@ -274,9 +274,9 @@ CONTEXT: JSON data, line 1: -- make sure jsonb is passed through json generators without being escaped SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); - array_to_json --------------------------- - [{"a": 1},{"b": [2, 3]}] + array_to_json +--------------------------- + [{"a": 1}, {"b": [2, 3]}] (1 row) -- to_jsonb, timestamps diff -aur postgresql-9.5-9.5.3.orig/src/test/regress/expected/rangefuncs.out postgresql-9.5-9.5.3/src/test/regress/expected/rangefuncs.out --- postgresql-9.5-9.5.3.orig/src/test/regress/expected/rangefuncs.out 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/test/regress/expected/rangefuncs.out 2016-06-10 11:17:16.540427758 +1000 @@ -68,12 +68,12 @@ (1 row) select row_to_json(s.*) from generate_series(11,14) with ordinality s; - row_to_json -------------------------- - {"s":11,"ordinality":1} - {"s":12,"ordinality":2} - {"s":13,"ordinality":3} - {"s":14,"ordinality":4} + row_to_json +---------------------------- + {"s": 11, "ordinality": 1} + {"s": 12, "ordinality": 2} + {"s": 13, "ordinality": 3} + {"s": 14, "ordinality": 4} (4 rows) -- ordinality vs. views diff -aur postgresql-9.5-9.5.3.orig/src/test/regress/expected/rowtypes.out postgresql-9.5-9.5.3/src/test/regress/expected/rowtypes.out --- postgresql-9.5-9.5.3.orig/src/test/regress/expected/rowtypes.out 2016-05-10 06:50:23.000000000 +1000 +++ postgresql-9.5-9.5.3/src/test/regress/expected/rowtypes.out 2016-06-10 11:25:19.720376200 +1000 @@ -502,110 +502,110 @@ -- (bug #11210 and other reports) -- select row_to_json(i) from int8_tbl i; - row_to_json ------------------------------------------------- - {"q1":123,"q2":456} - {"q1":123,"q2":4567890123456789} - {"q1":4567890123456789,"q2":123} - {"q1":4567890123456789,"q2":4567890123456789} - {"q1":4567890123456789,"q2":-4567890123456789} + row_to_json +--------------------------------------------------- + {"q1": 123, "q2": 456} + {"q1": 123, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": 123} + {"q1": 4567890123456789, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": -4567890123456789} (5 rows) select row_to_json(i) from int8_tbl i(x,y); - row_to_json ----------------------------------------------- - {"x":123,"y":456} - {"x":123,"y":4567890123456789} - {"x":4567890123456789,"y":123} - {"x":4567890123456789,"y":4567890123456789} - {"x":4567890123456789,"y":-4567890123456789} + row_to_json +------------------------------------------------- + {"x": 123, "y": 456} + {"x": 123, "y": 4567890123456789} + {"x": 4567890123456789, "y": 123} + {"x": 4567890123456789, "y": 4567890123456789} + {"x": 4567890123456789, "y": -4567890123456789} (5 rows) create temp view vv1 as select * from int8_tbl; select row_to_json(i) from vv1 i; - row_to_json ------------------------------------------------- - {"q1":123,"q2":456} - {"q1":123,"q2":4567890123456789} - {"q1":4567890123456789,"q2":123} - {"q1":4567890123456789,"q2":4567890123456789} - {"q1":4567890123456789,"q2":-4567890123456789} + row_to_json +--------------------------------------------------- + {"q1": 123, "q2": 456} + {"q1": 123, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": 123} + {"q1": 4567890123456789, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": -4567890123456789} (5 rows) select row_to_json(i) from vv1 i(x,y); - row_to_json ----------------------------------------------- - {"x":123,"y":456} - {"x":123,"y":4567890123456789} - {"x":4567890123456789,"y":123} - {"x":4567890123456789,"y":4567890123456789} - {"x":4567890123456789,"y":-4567890123456789} + row_to_json +------------------------------------------------- + {"x": 123, "y": 456} + {"x": 123, "y": 4567890123456789} + {"x": 4567890123456789, "y": 123} + {"x": 4567890123456789, "y": 4567890123456789} + {"x": 4567890123456789, "y": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1, q2 from int8_tbl) as ss; - row_to_json ------------------------------------------------- - {"q1":123,"q2":456} - {"q1":123,"q2":4567890123456789} - {"q1":4567890123456789,"q2":123} - {"q1":4567890123456789,"q2":4567890123456789} - {"q1":4567890123456789,"q2":-4567890123456789} + row_to_json +--------------------------------------------------- + {"q1": 123, "q2": 456} + {"q1": 123, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": 123} + {"q1": 4567890123456789, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1, q2 from int8_tbl offset 0) as ss; - row_to_json ------------------------------------------------- - {"q1":123,"q2":456} - {"q1":123,"q2":4567890123456789} - {"q1":4567890123456789,"q2":123} - {"q1":4567890123456789,"q2":4567890123456789} - {"q1":4567890123456789,"q2":-4567890123456789} + row_to_json +--------------------------------------------------- + {"q1": 123, "q2": 456} + {"q1": 123, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": 123} + {"q1": 4567890123456789, "q2": 4567890123456789} + {"q1": 4567890123456789, "q2": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1 as a, q2 as b from int8_tbl) as ss; - row_to_json ----------------------------------------------- - {"a":123,"b":456} - {"a":123,"b":4567890123456789} - {"a":4567890123456789,"b":123} - {"a":4567890123456789,"b":4567890123456789} - {"a":4567890123456789,"b":-4567890123456789} + row_to_json +------------------------------------------------- + {"a": 123, "b": 456} + {"a": 123, "b": 4567890123456789} + {"a": 4567890123456789, "b": 123} + {"a": 4567890123456789, "b": 4567890123456789} + {"a": 4567890123456789, "b": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1 as a, q2 as b from int8_tbl offset 0) as ss; - row_to_json ----------------------------------------------- - {"a":123,"b":456} - {"a":123,"b":4567890123456789} - {"a":4567890123456789,"b":123} - {"a":4567890123456789,"b":4567890123456789} - {"a":4567890123456789,"b":-4567890123456789} + row_to_json +------------------------------------------------- + {"a": 123, "b": 456} + {"a": 123, "b": 4567890123456789} + {"a": 4567890123456789, "b": 123} + {"a": 4567890123456789, "b": 4567890123456789} + {"a": 4567890123456789, "b": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1 as a, q2 as b from int8_tbl) as ss(x,y); - row_to_json ----------------------------------------------- - {"x":123,"y":456} - {"x":123,"y":4567890123456789} - {"x":4567890123456789,"y":123} - {"x":4567890123456789,"y":4567890123456789} - {"x":4567890123456789,"y":-4567890123456789} + row_to_json +------------------------------------------------- + {"x": 123, "y": 456} + {"x": 123, "y": 4567890123456789} + {"x": 4567890123456789, "y": 123} + {"x": 4567890123456789, "y": 4567890123456789} + {"x": 4567890123456789, "y": -4567890123456789} (5 rows) select row_to_json(ss) from (select q1 as a, q2 as b from int8_tbl offset 0) as ss(x,y); - row_to_json ----------------------------------------------- - {"x":123,"y":456} - {"x":123,"y":4567890123456789} - {"x":4567890123456789,"y":123} - {"x":4567890123456789,"y":4567890123456789} - {"x":4567890123456789,"y":-4567890123456789} + row_to_json +------------------------------------------------- + {"x": 123, "y": 456} + {"x": 123, "y": 4567890123456789} + {"x": 4567890123456789, "y": 123} + {"x": 4567890123456789, "y": 4567890123456789} + {"x": 4567890123456789, "y": -4567890123456789} (5 rows) explain (costs off) @@ -622,38 +622,38 @@ select row_to_json(q) from (select thousand, tenthous from tenk1 where thousand = 42 and tenthous < 2000 offset 0) q; - row_to_json ---------------------------------- - {"thousand":42,"tenthous":42} - {"thousand":42,"tenthous":1042} + row_to_json +------------------------------------ + {"thousand": 42, "tenthous": 42} + {"thousand": 42, "tenthous": 1042} (2 rows) select row_to_json(q) from (select thousand as x, tenthous as y from tenk1 where thousand = 42 and tenthous < 2000 offset 0) q; - row_to_json -------------------- - {"x":42,"y":42} - {"x":42,"y":1042} + row_to_json +---------------------- + {"x": 42, "y": 42} + {"x": 42, "y": 1042} (2 rows) select row_to_json(q) from (select thousand as x, tenthous as y from tenk1 where thousand = 42 and tenthous < 2000 offset 0) q(a,b); - row_to_json -------------------- - {"a":42,"b":42} - {"a":42,"b":1042} + row_to_json +---------------------- + {"a": 42, "b": 42} + {"a": 42, "b": 1042} (2 rows) create temp table tt1 as select * from int8_tbl limit 2; create temp table tt2 () inherits(tt1); insert into tt2 values(0,0); select row_to_json(r) from (select q2,q1 from tt1 offset 0) r; - row_to_json ----------------------------------- - {"q2":456,"q1":123} - {"q2":4567890123456789,"q1":123} - {"q2":0,"q1":0} + row_to_json +------------------------------------- + {"q2": 456, "q1": 123} + {"q2": 4567890123456789, "q1": 123} + {"q2": 0, "q1": 0} (3 rows)
^ permalink raw reply [nested|flat] 9+ messages in thread


end of thread, other threads:[~2016-06-10 11:06 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-07 01:42 BUG #14178: output of jsonb_object and json_object doesn't match textually ap@zip.com.au
2016-06-07 02:40 ` Michael Paquier <michael.paquier@gmail.com>
2016-06-07 06:48   ` Thomas Munro <thomas.munro@enterprisedb.com>
2016-06-07 06:54     ` Michael Paquier <michael.paquier@gmail.com>
2016-06-07 07:55       ` Thomas Munro <thomas.munro@enterprisedb.com>
2016-06-07 11:58         ` Michael Paquier <michael.paquier@gmail.com>
2016-06-08 02:59           ` Michael Paquier <michael.paquier@gmail.com>
2016-06-08 10:32             ` AP <ap@zip.com.au>
2016-06-10 11:06               ` AP <ap@zip.com.au>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox