json_strip_nulls()

From: Erwin Brandstetter <brsaweda(at)gmail(dot)com>
To: pgsql-docs(at)postgresql(dot)org
Subject: json_strip_nulls()
Date: 2022-01-22 19:10:46
Message-ID: CAGHENJ5OckBWa22Cf=N9e68EZM2=_S8AWjWF-_xnGa+iktUh7g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

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

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message David G. Johnston 2022-01-22 19:31:10 Re: json_strip_nulls()
Previous Message PG Doc comments form 2022-01-22 16:49:35 array_to_string and string_to_array