PostgreSQL jsonb_strip_nulls() Function

The PostgreSQL jsonb_strip_nulls() function removes fields with null values ​​in the specified JSONB object recursively, and null values ​​in non-object fields are not processed.

jsonb_strip_nulls() Syntax

This is the syntax of the PostgreSQL jsonb_strip_nulls() function:

jsonb_strip_nulls(json_value JSONB) -> JSONB

Parameters

json_value

Required. The JSONB value to process.

Return value

The PostgreSQL jsonb_strip_nulls() function returns the given JSONB value with null fields are removed recursively.

If you provide a NULL parameter, the jsonb_strip_nulls() function will return NULL.

jsonb_strip_nulls() Examples

The following example shows how to use the PostgreSQL jsonb_strip_nulls() function to remove a null object field from a given JSONB value.

SELECT jsonb_strip_nulls('[1, null, 3, {"x": 1, "y": null}]');
   jsonb_strip_nulls
------------------------
 [1, null, 3, {"x": 1}]

Here, only the field y in the JOSN object {"x": 1, "y": null} are removed, and the null values ​​in the array are not removed.