Re: to_json(NULL) should to return JSON null instead NULL

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: "Shulgin, Oleksandr" <oleksandr(dot)shulgin(at)zalando(dot)de>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: to_json(NULL) should to return JSON null instead NULL
Date: 2015-08-29 13:39:00
Message-ID: 30640.1440855540@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> On 08/29/2015 08:47 AM, Shulgin, Oleksandr wrote:
>> Given there were no loud complaints about this, the current behavior
>> is appropriate for most users, the rest can still work around using
>> coalesce(to_json(...), json 'null').

> I don't think it's necessarily more correct. But I do agree that it's
> not a good idea to change the behaviour unless there is major
> unhappiness with it.

I'm not entirely convinced that JSON NULL and SQL NULL should be treated
as the same concept, so I would say that the current behavior is fine ---
at least when you think about it in isolation. However, haven't we
already bought into that equivalence in these examples?

regression=# select row_to_json(row(1,null,2));
row_to_json
---------------------------
{"f1":1,"f2":null,"f3":2}
(1 row)

regression=# select array_to_json(array[1,null,2]);
array_to_json
---------------
[1,null,2]
(1 row)

or even in to_json itself:

regression=# select to_json(array[1,null,2]);
to_json
------------
[1,null,2]
(1 row)

The scalar case is definitely failing to be consistent with these.
Is consistency a sufficient reason to change it?

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Shulgin, Oleksandr 2015-08-29 13:43:28 Re: to_json(NULL) should to return JSON null instead NULL
Previous Message Christoph Berg 2015-08-29 13:38:51 Re: 9.4 broken on alpha