The manual says this about json_strip_nulls()[1]:

> Deletes all object fields that have null values from the given JSON
value, recursively. Null values that are not object fields are untouched.

[1]
https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE

But the function also strips all insignificant white space:

test=> SELECT json_strip_nulls(json '{"a": 1 ,
test'>                         "foo"  :  "bar"
test'>                         }');
  json_strip_nulls
---------------------
 {"a":1,"foo":"bar"}

This is a useful feature to trim noise from json values, but unreliable
while undocumented. So let's document the behavior:


*Deletes all object fields that have null values from the given JSON value,
recursively. Null values that are not object fields are untouched.
json_strip_nulls additionally removes all insignificant white space.*

If that's undesirable, let's remove the functionality - and provide a
dedicated function for the task.
I found similar (unresolved) considerations here:

https://www.postgresql.org/message-id/20160610110633.GG18838%40zip.com.au

There were related requests on Stackoverflow:
-
https://stackoverflow.com/questions/27505181/fetching-compact-version-of-jsonb-in-postgresql
-
https://stackoverflow.com/questions/70813106/jsonb-cast-to-text-without-sporious-spaces


Regards
Erwin

Reply via email to