pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #15763: JSON nulls not handled properly
5+ messages / 4 participants
[nested] [flat]

* BUG #15763: JSON nulls not handled properly
@ 2019-04-16 19:13 PG Bug reporting form <noreply@postgresql.org>
  2019-04-16 19:29 ` Re: BUG #15763: JSON nulls not handled properly Tom Lane <tgl@sss.pgh.pa.us>
  2019-04-16 19:35 ` Re: BUG #15763: JSON nulls not handled properly David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 5+ messages in thread

From: PG Bug reporting form @ 2019-04-16 19:13 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: jacobcrell@gmail.com

The following bug has been logged on the website:

Bug reference:      15763
Logged by:          Jacob Crell
Email address:      jacobcrell@gmail.com
PostgreSQL version: 9.6.8
Operating system:   AWS RDS
Description:        

SELECT '{"test":null}'::json->>'test' will return a null
SELECT '{"test":null}'::json->'test' will return a string 'null'
The 2nd option seems like it should also return a null.



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

* Re: BUG #15763: JSON nulls not handled properly
  2019-04-16 19:13 BUG #15763: JSON nulls not handled properly PG Bug reporting form <noreply@postgresql.org>
@ 2019-04-16 19:29 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2019-04-16 19:29 UTC (permalink / raw)
  To: jacobcrell@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> writes:
> SELECT '{"test":null}'::json->>'test' will return a null
> SELECT '{"test":null}'::json->'test' will return a string 'null'
> The 2nd option seems like it should also return a null.

I don't claim to be a JSON expert, but the -> operator is specified
to give back a JSON value (not a text string).  So 'null'::json seems
like the right answer there.  Also, if we had it return a NULL, then
you couldn't distinguish the case where the field isn't present:

regression=# SELECT '{"test":null}'::json->'notthere';
 ?column? 
----------
 
(1 row)

regression=# SELECT '{"test":null}'::json->'notthere' is null;
 ?column? 
----------
 t
(1 row)

			regards, tom lane





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

* Re: BUG #15763: JSON nulls not handled properly
  2019-04-16 19:13 BUG #15763: JSON nulls not handled properly PG Bug reporting form <noreply@postgresql.org>
@ 2019-04-16 19:35 ` David G. Johnston <david.g.johnston@gmail.com>
  2019-04-16 19:57   ` Re: BUG #15763: JSON nulls not handled properly Jacob Crell <jacobcrell@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2019-04-16 19:35 UTC (permalink / raw)
  To: jacobcrell@gmail.com <jacobcrell@gmail.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

On Tuesday, April 16, 2019, PG Bug reporting form <noreply@postgresql.org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference:      15763
> Logged by:          Jacob Crell
> Email address:      jacobcrell@gmail.com
> PostgreSQL version: 9.6.8
> Operating system:   AWS RDS
> Description:
>
> SELECT '{"test":null}'::json->>'test' will return a null
> SELECT '{"test":null}'::json->'test' will return a string 'null'
> The 2nd option seems like it should also return a null.
>

This seems under documented but I can confidently say the behavior shown is
intended and thus not a bug.  Nor should it be changed.  The second example
returns a json typed value that when printed as text is the character
sequence null.  It does not return a PostgreSQL string type.

Conversion of json null to PostgreSQL text results in a NULL of type text,
which is indeed the first outcome.  This is, however, a lossy one-way
conversion since NULL::json is NULL, not ‘null’.

David J.

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

* Re: BUG #15763: JSON nulls not handled properly
  2019-04-16 19:13 BUG #15763: JSON nulls not handled properly PG Bug reporting form <noreply@postgresql.org>
  2019-04-16 19:35 ` Re: BUG #15763: JSON nulls not handled properly David G. Johnston <david.g.johnston@gmail.com>
@ 2019-04-16 19:57   ` Jacob Crell <jacobcrell@gmail.com>
  2019-04-16 20:10     ` Re: BUG #15763: JSON nulls not handled properly Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Jacob Crell @ 2019-04-16 19:57 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

Thanks for the response. I may have been a bit off in my diagnosis of what
was going wrong. My bug report stemmed from the fact that the below returns
different results, the first throwing an error and the second returning no
rows:

SELECT json_array_elements('{"key":null}'::json->'key')
SELECT json_array_elements(null::json)

This seems unintuitive. Is it potentially a bug?

On Tue, Apr 16, 2019 at 2:35 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Tuesday, April 16, 2019, PG Bug reporting form <noreply@postgresql.org>
> wrote:
>
>> The following bug has been logged on the website:
>>
>> Bug reference:      15763
>> Logged by:          Jacob Crell
>> Email address:      jacobcrell@gmail.com
>> PostgreSQL version: 9.6.8
>> Operating system:   AWS RDS
>> Description:
>>
>> SELECT '{"test":null}'::json->>'test' will return a null
>> SELECT '{"test":null}'::json->'test' will return a string 'null'
>> The 2nd option seems like it should also return a null.
>>
>
> This seems under documented but I can confidently say the behavior shown
> is intended and thus not a bug.  Nor should it be changed.  The second
> example returns a json typed value that when printed as text is the
> character sequence null.  It does not return a PostgreSQL string type.
>
> Conversion of json null to PostgreSQL text results in a NULL of type text,
> which is indeed the first outcome.  This is, however, a lossy one-way
> conversion since NULL::json is NULL, not ‘null’.
>
> David J.
>
>

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

* Re: BUG #15763: JSON nulls not handled properly
  2019-04-16 19:13 BUG #15763: JSON nulls not handled properly PG Bug reporting form <noreply@postgresql.org>
  2019-04-16 19:35 ` Re: BUG #15763: JSON nulls not handled properly David G. Johnston <david.g.johnston@gmail.com>
  2019-04-16 19:57   ` Re: BUG #15763: JSON nulls not handled properly Jacob Crell <jacobcrell@gmail.com>
@ 2019-04-16 20:10     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2019-04-16 20:10 UTC (permalink / raw)
  To: Jacob Crell <jacobcrell@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

Jacob Crell <jacobcrell@gmail.com> writes:
> Thanks for the response. I may have been a bit off in my diagnosis of what
> was going wrong. My bug report stemmed from the fact that the below returns
> different results, the first throwing an error and the second returning no
> rows:

> SELECT json_array_elements('{"key":null}'::json->'key')
> SELECT json_array_elements(null::json)

> This seems unintuitive. Is it potentially a bug?

No, because null::json is not the same thing as 'null'::json.
They're related ideas, but not interchangeable.  In your
second example, json_array_elements() never gets called at all
because it's marked strict and strict functions are not invoked
on SQL nulls.  In the first example, it is called and it complains
because the JSON value it's called on isn't an array, but a
scalar null value.

You could make a case that the function should have been defined
to return zero rows for a JSON-null input ... but it'd be at best
a debatable point, so we're unlikely to change the function
definition now.

If you need that behavior, it is available in the jsonb world,
with something like

regression=# SELECT jsonb_array_elements(nullif('{"key":null}'::jsonb->'key',
                                                'null'::jsonb));
 jsonb_array_elements 
----------------------
(0 rows)

But nullif() doesn't work on plain json, for lack of an equality
operator :-(

			regards, tom lane





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


end of thread, other threads:[~2019-04-16 20:10 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16 19:13 BUG #15763: JSON nulls not handled properly PG Bug reporting form <noreply@postgresql.org>
2019-04-16 19:29 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-04-16 19:35 ` David G. Johnston <david.g.johnston@gmail.com>
2019-04-16 19:57   ` Jacob Crell <jacobcrell@gmail.com>
2019-04-16 20:10     ` Tom Lane <tgl@sss.pgh.pa.us>

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