pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Bryn Llewellyn <bryn@yugabyte.com>
Cc: Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: mmoncure@gmail.com, pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: '{"x": 42, "y": null}'::jsonb != '{"x": 42}'::jsonb ... Really?
Date: Sat, 18 Jun 2022 12:28:01 +0200
Message-ID: <F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com> (raw)
In-Reply-To: <780CD5C2-B5B4-4C07-A9E7-33E1352D441A@yugabyte.com>
References: <7E0BF7DF-CC12-48B6-8550-9C2B5A8F78B1@yugabyte.com>
	<CAKFQuwbrQGuGVdZR=aNOPAhoGYJiqvJK69vFk3w4oQ1_apuT8w@mail.gmail.com>
	<C5A701D6-50AC-4561-B64C-318DBF52A312@yugabyte.com>
	<426968.1654321063@sss.pgh.pa.us>
	<780CD5C2-B5B4-4C07-A9E7-33E1352D441A@yugabyte.com>


> On 18 Jun 2022, at 2:14, Bryn Llewellyn <bryn@yugabyte.com> wrote:
> 
> I implemented two complementary functions:
> 
> —"no_null_keys()" checks that a "jsonb" value has no occurrences of « "some key": null »
> 
> —"strip_null_keys()" removes « "some key": null » occurrences from a "jsonb" value
> 
> The code checks with "no_null_keys()" that, as expected, no ingested JSON document has an occurrence of « "some key": null ».
> 
> And it uses "strip_null_keys()" on the output of "to_jsonb()" — and, as appropriate, any other built-in JSON function that produces a "jsonb" value.
> 
> It was straightforward to implement these two functions by using REGEXP built-in functionality on the canonically formatted "text" value produced by the "jsonb::text" typecast.

In my experience, using regular expressions applied to document formats tends to get you false positives. I’d be worried about books with titles similar to 'How we wrote a regular expression to detect occurrences of "some key": null in our JSON documents', for example.

For stripping those null occurrences, you are aware of the json_strip_nulls(json) and jsonb_strip_nulls(jsonb) functions, right?

For detecting them on a recent PG, the @? operator or json_path_exists(json, jsonpath) functions would probably do the trick.
I am not too familiar with JSONPATH expressions, but I expect (it passed some preliminary testing) this would detect your nulls just fine, while taking JSON semantics into account:

jsonb_path_exists(document, '$.** ? (@ == null)'::jsonpath)

For PG-specifics on JSONPATH, see section 9.16.2 on: https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-SQLJSON-OP-TABLE

A recursive query is another possible solution. It would probably perform far worse, but I find them more rewarding to write. Some people prefer Sodoku.

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (12+ messages)  latest in thread

Message-ID: <F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com>
Permalink:  ../F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com/
Also on:    postgresql.org/message-id/F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, bryn@yugabyte.com, tgl@sss.pgh.pa.us, david.g.johnston@gmail.com
  Subject: Re: '{"x": 42, "y": null}'::jsonb != '{"x": 42}'::jsonb ... Really?
  In-Reply-To: <F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com>
  References: <7E0BF7DF-CC12-48B6-8550-9C2B5A8F78B1@yugabyte.com> <CAKFQuwbrQGuGVdZR=aNOPAhoGYJiqvJK69vFk3w4oQ1_apuT8w@mail.gmail.com> <C5A701D6-50AC-4561-B64C-318DBF52A312@yugabyte.com> <426968.1654321063@sss.pgh.pa.us> <780CD5C2-B5B4-4C07-A9E7-33E1352D441A@yugabyte.com> <F24FA846-001B-46A2-A399-59A1B72AB78A@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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