1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-21 05:21:08 +03:00

Allow json{b}_strip_nulls to remove null array elements

An additional paramater ("strip_in_arrays") is added to these functions.
It defaults to false. If true, then null array elements are removed as
well as null valued object fields. JSON that just consists of a single
null is not affected.

Author: Florents Tselai <florents.tselai@gmail.com>

Discussion: https://postgr.es/m/4BCECCD5-4F40-4313-9E98-9E16BEB0B01D@gmail.com
This commit is contained in:
Andrew Dunstan
2025-03-05 09:50:34 -05:00
parent 5ead85fbc8
commit 4603903d29
8 changed files with 190 additions and 9 deletions

View File

@@ -2504,6 +2504,56 @@ select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
{"a":{},"d":{}}
(1 row)
-- json_strip_nulls (strip_in_arrays=true)
select json_strip_nulls(null, true);
json_strip_nulls
------------------
(1 row)
select json_strip_nulls('1', true);
json_strip_nulls
------------------
1
(1 row)
select json_strip_nulls('"a string"', true);
json_strip_nulls
------------------
"a string"
(1 row)
select json_strip_nulls('null', true);
json_strip_nulls
------------------
null
(1 row)
select json_strip_nulls('[1,2,null,3,4]', true);
json_strip_nulls
------------------
[1,2,3,4]
(1 row)
select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}', true);
json_strip_nulls
-------------------------------
{"a":1,"c":[2,3],"d":{"e":4}}
(1 row)
select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]', true);
json_strip_nulls
---------------------
[1,{"a":1,"c":2},3]
(1 row)
-- an empty object is not null and should not be stripped
select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }', true);
json_strip_nulls
------------------
{"a":{},"d":{}}
(1 row)
-- json to tsvector
select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
to_tsvector

View File

@@ -4153,6 +4153,56 @@ select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
{"a": {}, "d": {}}
(1 row)
-- jsonb_strip_nulls (strip_in_arrays=true)
select jsonb_strip_nulls(null, true);
jsonb_strip_nulls
-------------------
(1 row)
select jsonb_strip_nulls('1', true);
jsonb_strip_nulls
-------------------
1
(1 row)
select jsonb_strip_nulls('"a string"', true);
jsonb_strip_nulls
-------------------
"a string"
(1 row)
select jsonb_strip_nulls('null', true);
jsonb_strip_nulls
-------------------
null
(1 row)
select jsonb_strip_nulls('[1,2,null,3,4]', true);
jsonb_strip_nulls
-------------------
[1, 2, 3, 4]
(1 row)
select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}', true);
jsonb_strip_nulls
--------------------------------------
{"a": 1, "c": [2, 3], "d": {"e": 4}}
(1 row)
select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]', true);
jsonb_strip_nulls
--------------------------
[1, {"a": 1, "c": 2}, 3]
(1 row)
-- an empty object is not null and should not be stripped
select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }', true);
jsonb_strip_nulls
--------------------
{"a": {}, "d": {}}
(1 row)
select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
jsonb_pretty
----------------------------

View File

@@ -814,6 +814,25 @@ select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
-- an empty object is not null and should not be stripped
select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
-- json_strip_nulls (strip_in_arrays=true)
select json_strip_nulls(null, true);
select json_strip_nulls('1', true);
select json_strip_nulls('"a string"', true);
select json_strip_nulls('null', true);
select json_strip_nulls('[1,2,null,3,4]', true);
select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}', true);
select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]', true);
-- an empty object is not null and should not be stripped
select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }', true);
-- json to tsvector
select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);

View File

@@ -1102,6 +1102,24 @@ select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
-- an empty object is not null and should not be stripped
select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
-- jsonb_strip_nulls (strip_in_arrays=true)
select jsonb_strip_nulls(null, true);
select jsonb_strip_nulls('1', true);
select jsonb_strip_nulls('"a string"', true);
select jsonb_strip_nulls('null', true);
select jsonb_strip_nulls('[1,2,null,3,4]', true);
select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}', true);
select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]', true);
-- an empty object is not null and should not be stripped
select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }', true);
select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');