https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-SQLJSON-OP-TABLE
The "G", "E", "L", and "O" keys on my keyboard were broken. They're fixed now, and so "postgres strip keys with null value from jsonb object" got me to this (but, strangely, not to the PG docs): This is embarrassing. I wrote that doc, along with the rest of the content in the "JSON data types and functionality" section just before COVID hit us. Apparently, life under lockdown has damaged my corpus callosum. I typed up my proof-of-concept code and emails with one half of my brain—and it was the other half that wrote that documentation. So thanks for the memory jog. My excuse is that (as was the case with my malformed ISBN that Peter Holzer pointed out in a separate thread) I wanted just to show myself, as a proof-of-concept, that stripping nulls was feasible—so I gave it no more thought once I'd done that. But, I suppose, that's not excuse... Anyway, my "strip_null_keys()" is already on the scrapheap. And the body of my "no_null_keys()" reduced to a single line: create function no_null_keys(j in jsonb) returns boolean immutable language sql as $body$ select j = jsonb_strip_nulls(j); $body$; You might argue that I don't need to bother with the encapsulation. But it makes testing easier—and I'm trusting that inlining works as advertised. Your point about false positives is well taken. So, just for sport: create type t1 as (k int, v text); create type t2 as (a int, b int, c t1, d t1, e text[]); create function j() returns jsonb language plpgsql as $body$ declare t constant text not null := 'How we wrote a regular _expression_ to detect occurrences of « "some key": null » in our JSON documents!'; c1 constant t1 not null := (17, t); c2 constant t1 not null := (29, null); arr constant text[] not null := array['x', null::text, t]; r constant t2 not null := (42, null, c1, c2, arr); begin return to_jsonb(r); end; $body$; select jsonb_pretty(j()); The output includes two occurrences of this: "How we wrote a regular _expression_ to detect occurrences of « \"some key\": null » in our JSON documents!" I believe that the "jsonb" to "text" conversion never produces an isolated double-quote within the representation of an object key's value. I checked that my "strip_null_keys()" handled your example before consigning it to the scrapheap—and it didn't let me down. But it would be foolish to argue that there isn't some way to provoke a false positive. |