Re: jsonb_set() strictness considered harmful to data

From: Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: Ariadne Conill <ariadne(at)dereferenced(dot)org>, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Christoph Moench-Tegeder <cmt(at)burggraben(dot)net>, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>, feld(at)freebsd(dot)org
Subject: Re: jsonb_set() strictness considered harmful to data
Date: 2019-10-20 21:10:15
Message-ID: CA+renyW3jsJSoMtsa0PrvQhj9iTu5=Y-oVbAi5A92+CrgdU9TQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

> That said, I think it is reasonable that a PostgreSQL JSON function
> behaves in the way that JSON users would expect, so here is my +1 for
> interpreting an SQL NULL as a JSON null in the above case

Just to chime in as another application developer: the current
functionality does seem pretty surprising and dangerous to me. Raising
an exception seems pretty annoying. Setting the key's value to a JSON
null would be fine, but I also like the idea of removing the key
entirely, since that gives you strictly more functionality: you can
always set the key to a JSON null by passing one in, if that's what
you want. But there are lots of other functions that convert SQL NULL
to JSON null:

postgres=# select row_to_json(row(null)), json_build_object('foo',
null), json_object(array['foo', null]), json_object(array['foo'],
array[null]);
row_to_json | json_build_object | json_object | json_object
-------------+-------------------+----------------+----------------
{"f1":null} | {"foo" : null} | {"foo" : null} | {"foo" : null}
(1 row)

(The jsonb variants give the same results.)

I think those functions are very similar to json_set here, and I'd
expect json_set to do what they do (i.e. convert SQL NULL to JSON
null).

Paul

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Dunstan 2019-10-20 22:51:05 Re: jsonb_set() strictness considered harmful to data
Previous Message John W Higgins 2019-10-20 21:09:36 Re: jsonb_set() strictness considered harmful to data

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2019-10-20 22:07:02 Re: Clean up MinGW def file generation
Previous Message John W Higgins 2019-10-20 21:09:36 Re: jsonb_set() strictness considered harmful to data