jsonb_strip_nulls with arrays?

Lists: pgsql-hackers
From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: jsonb_strip_nulls with arrays?
Date: 2024-09-17 09:26:36
Message-ID: 4BCECCD5-4F40-4313-9E98-9E16BEB0B01D@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Currently:

jsonb_strip_nulls ( jsonb ) → jsonb
Deletes all object fields that have null values from the given JSON value, recursively. Null values that are not object fields are untouched.

> Null values that are not object fields are untouched.

Can we revisit this and make it work with arrays, too?
Tbh, at first sight that looked like the expected behavior for me.
That is strip nulls from arrays as well.

This has been available since 9.5 and iiuc predates lots of the jsonb array work.

In practice, though, whenever jsonb_build_array is used (especially with jsonpath),
a few nulls do appear in the resulting array most of the times,
Currently, there’s no expressive way to remove this.

We could also have jsonb_array_strip_nulls(jsonb) as well


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2024-09-17 14:11:47
Message-ID: 92de2543-de33-4092-9de7-b532a078353f@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


On 2024-09-17 Tu 5:26 AM, Florents Tselai wrote:
>
> Currently:
>
>
> |jsonb_strip_nulls| ( |jsonb| ) → |jsonb|
>
> Deletes all object fields that have null values from the given JSON
> value, recursively. Null values that are not object fields are untouched.
>
>
> > Null values that are not object fields are untouched.
>
>
> Can we revisit this and make it work with arrays, too?
>
> Tbh, at first sight that looked like the expected behavior for me.
>
> That is strip nulls from arrays as well.
>
>
> This has been available since 9.5 and iiuc predates lots of the jsonb
> array work.
>

I don't think that's a great idea. Removing an object field which has a
null value shouldn't have any effect on the surrounding data, nor really
any on other operations (If you try to get the value of the missing
field it should give you back null). But removing a null array member
isn't like that at all - unless it's the trailing member of the array it
will renumber all the succeeding array members.

And I don't think we should be changing the behaviour of a function,
that people might have been relying on for the better part of a decade.

>
> In practice, though, whenever jsonb_build_array is used (especially
> with jsonpath),
>
> a few nulls do appear in the resulting array most of the times,
>
> Currently, there’s no expressive way to remove this.
>
>
> We could also have jsonb_array_strip_nulls(jsonb) as well
>

We could, if we're going to do anything at all in this area. Another
possibility would be to provide a second optional parameter for
json{b}_strip_nulls. That's probably a better way to go.

cheers

andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com


From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2024-09-17 20:53:58
Message-ID: CA+v5N43=48Ddg=Ub313bX3g2qu9VRHzfTP3TAGN10WF6XNX_2w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Sep 17, 2024 at 5:11 PM Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:

>
> On 2024-09-17 Tu 5:26 AM, Florents Tselai wrote:
>
> Currently:
>
>
> jsonb_strip_nulls ( jsonb ) → jsonb
>
> Deletes all object fields that have null values from the given JSON value,
> recursively. Null values that are not object fields are untouched.
>
>
> > Null values that are not object fields are untouched.
>
>
> Can we revisit this and make it work with arrays, too?
>
> Tbh, at first sight that looked like the expected behavior for me.
>
> That is strip nulls from arrays as well.
>
>
> This has been available since 9.5 and iiuc predates lots of the jsonb
> array work.
>
>
> I don't think that's a great idea. Removing an object field which has a
> null value shouldn't have any effect on the surrounding data, nor really
> any on other operations (If you try to get the value of the missing field
> it should give you back null). But removing a null array member isn't like
> that at all - unless it's the trailing member of the array it will renumber
> all the succeeding array members.
>
> And I don't think we should be changing the behaviour of a function, that
> people might have been relying on for the better part of a decade.
>
>
>
> In practice, though, whenever jsonb_build_array is used (especially with
> jsonpath),
>
> a few nulls do appear in the resulting array most of the times,
>
> Currently, there’s no expressive way to remove this.
>
>
> We could also have jsonb_array_strip_nulls(jsonb) as well
>
>
> We could, if we're going to do anything at all in this area. Another
> possibility would be to provide a second optional parameter for
> json{b}_strip_nulls. That's probably a better way to go.
>
Here's a patch that adds that argument (only for jsonb; no json
implementation yet)

That's how I imagined & implemented it,
but there may be non-obvious pitfalls in the semantics.

as-is version

select jsonb_strip_nulls('[1,2,null,3,4]');
jsonb_strip_nulls
--------------------
[1, 2, null, 3, 4]
(1 row)

select
jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
jsonb_strip_nulls
--------------------------------------------
{"a": 1, "c": [2, null, 3], "d": {"e": 4}}
(1 row)

with the additional boolean flag added

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)

GH PR view: https://github.com/Florents-Tselai/postgres/pull/6/files

> cheers
>
>
> andrew
>
>
> --
> Andrew Dunstan
> EDB: https://www.enterprisedb.com
>
>

Attachment Content-Type Size
v1-0002-Add-docs-for-strip_in_arrays-argument.patch application/octet-stream 2.1 KB
v1-0001-jsonb_strip_nulls-jsonb-bool-wip.patch application/octet-stream 5.8 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-01-08 16:45:02
Message-ID: 00e32809-ae7e-46cf-9b33-7323f5fc7784@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


On 2024-09-17 Tu 4:53 PM, Florents Tselai wrote:
>
> We could, if we're going to do anything at all in this area.
> Another possibility would be to provide a second optional
> parameter for json{b}_strip_nulls. That's probably a better way to go.
>
> Here's a patch that adds that argument (only for jsonb; no json
> implementation yet)
>
>

I think it looks sane. We're not stripping a top level null, which is
one thing I looked out for.

I think we need a json implementation as well, though.

cheers

andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com


From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-01-18 09:51:22
Message-ID: FBED3D5A-5AF7-40B5-BEDF-51842B0194C4@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

> On 8 Jan 2025, at 6:45 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
>
>
> On 2024-09-17 Tu 4:53 PM, Florents Tselai wrote:
>>
>>> We could, if we're going to do anything at all in this area. Another possibility would be to provide a second optional parameter for json{b}_strip_nulls. That's probably a better way to go.
>>>
>> Here's a patch that adds that argument (only for jsonb; no json implementation yet)
>>
>>
>
> I think it looks sane. We're not stripping a top level null, which is one thing I looked out for.
>
> I think we need a json implementation as well, though.
>
>
>
Thanks for having a Look, Andrew;
if there aren’t any other objections, I’ll come back with a json implementation too.
> cheers
>
>
>
> andrew
>
>
>
> --
> Andrew Dunstan
> EDB: https://www.enterprisedb.com <https://www.enterprisedb.com/>


From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-02-19 21:23:51
Message-ID: F4E9D398-399E-46C1-ABC1-725B7E49261C@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

> On 18 Jan 2025, at 11:51 AM, Florents Tselai <florents(dot)tselai(at)gmail(dot)com> wrote:
>
>
>
>> On 8 Jan 2025, at 6:45 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>>
>>
>>
>> On 2024-09-17 Tu 4:53 PM, Florents Tselai wrote:
>>>
>>>> We could, if we're going to do anything at all in this area. Another possibility would be to provide a second optional parameter for json{b}_strip_nulls. That's probably a better way to go.
>>>>
>>> Here's a patch that adds that argument (only for jsonb; no json implementation yet)
>>>
>>>
>>
>> I think it looks sane. We're not stripping a top level null, which is one thing I looked out for.
>>
>> I think we need a json implementation as well, though.
>>
>>
>>
> Thanks for having a Look, Andrew;
> if there aren’t any other objections, I’ll come back with a json implementation too.

Attached is a v2 patch with the missing json implementation.
jsonb one remains the same.


>>
>> cheers
>>
>>
>>
>> andrew
>>
>>
>>
>> --
>> Andrew Dunstan
>> EDB: https://www.enterprisedb.com <https://www.enterprisedb.com/>


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-02-19 22:18:58
Message-ID: 2a3a38fb-818f-491f-bcd5-0c0672c874f2@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


On 2025-02-19 We 4:23 PM, Florents Tselai wrote:
>
>
>> On 18 Jan 2025, at 11:51 AM, Florents Tselai
>> <florents(dot)tselai(at)gmail(dot)com> wrote:
>>
>>
>>
>>> On 8 Jan 2025, at 6:45 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>>>
>>>
>>> On 2024-09-17 Tu 4:53 PM, Florents Tselai wrote:
>>>>
>>>> We could, if we're going to do anything at all in this area.
>>>> Another possibility would be to provide a second optional
>>>> parameter for json{b}_strip_nulls. That's probably a better way
>>>> to go.
>>>>
>>>> Here's a patch that adds that argument (only for jsonb; no json
>>>> implementation yet)
>>>>
>>>>
>>>
>>> I think it looks sane. We're not stripping a top level null, which
>>> is one thing I looked out for.
>>>
>>> I think we need a json implementation as well, though.
>>>
>>>
>> Thanks for having a Look, Andrew;
>> if there aren’t any other objections, I’ll come back with a json
>> implementation too.
>
> Attached is a v2 patch with the missing json implementation.
> jsonb one remains the same.

Please add this to the next Commitfest at
https://commitfest.postgresql.org/52/

cheers

andrew

>
>
>
--
Andrew Dunstan
EDB:https://www.enterprisedb.com


From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-02-28 17:57:27
Message-ID: 90F4B634-BAD4-45E0-8D0A-4A02CC37D4E2@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

> On 20 Feb 2025, at 12:18 AM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
>
>
> On 2025-02-19 We 4:23 PM, Florents Tselai wrote:
>>
>>
>>> On 18 Jan 2025, at 11:51 AM, Florents Tselai <florents(dot)tselai(at)gmail(dot)com> <mailto:florents(dot)tselai(at)gmail(dot)com> wrote:
>>>
>>>
>>>
>>>> On 8 Jan 2025, at 6:45 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> <mailto:andrew(at)dunslane(dot)net> wrote:
>>>>
>>>>
>>>>
>>>> On 2024-09-17 Tu 4:53 PM, Florents Tselai wrote:
>>>>>
>>>>>> We could, if we're going to do anything at all in this area. Another possibility would be to provide a second optional parameter for json{b}_strip_nulls. That's probably a better way to go.
>>>>>>
>>>>> Here's a patch that adds that argument (only for jsonb; no json implementation yet)
>>>>>
>>>>>
>>>>
>>>> I think it looks sane. We're not stripping a top level null, which is one thing I looked out for.
>>>>
>>>> I think we need a json implementation as well, though.
>>>>
>>>>
>>>>
>>> Thanks for having a Look, Andrew;
>>> if there aren’t any other objections, I’ll come back with a json implementation too.
>>
>> Attached is a v2 patch with the missing json implementation.
>> jsonb one remains the same.
>
>
> Please add this to the next Commitfest at https://commitfest.postgresql.org/52/
>

Added ; thanks
https://commitfest.postgresql.org/patch/5260/
>
> cheers
>
>
>
> andrew
>
>
>
>>
>>
>>
> --
> Andrew Dunstan
> EDB: https://www.enterprisedb.com <https://www.enterprisedb.com/>


From: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-03-06 00:10:04
Message-ID: CAB8KJ=gYd5g0tcDc7oQecLkfQ0jBPBmE+QnyYQGk8MBDDXndEw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi

2025年3月1日(土) 2:58 Florents Tselai <florents(dot)tselai(at)gmail(dot)com>:
> Please add this to the next Commitfest at https://commitfest.postgresql.org/52/
>
>
> Added ; thanks
> https://commitfest.postgresql.org/patch/5260/

I see this was committed, but there's a small formatting error in the docs
(extra comma in the parameter list); patch attached.

Regards

Ian Barwick

Attachment Content-Type Size
json_strip_nulls-doc-fix.patch text/x-patch 1.3 KB

From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-03-06 06:30:25
Message-ID: 88898C9B-B15D-46A0-830A-8316DDE0584F@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

> On 6 Mar 2025, at 2:10 AM, Ian Lawrence Barwick <barwick(at)gmail(dot)com> wrote:
>
> Hi
>
> 2025年3月1日(土) 2:58 Florents Tselai <florents(dot)tselai(at)gmail(dot)com>:
>> Please add this to the next Commitfest at https://commitfest.postgresql.org/52/
>>
>>
>> Added ; thanks
>> https://commitfest.postgresql.org/patch/5260/
>
> I see this was committed, but there's a small formatting error in the docs
> (extra comma in the parameter list); patch attached.
>
> Regards
>
> Ian Barwick
> <json_strip_nulls-doc-fix.patch>

You’re corrrect.


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Ian Lawrence Barwick <barwick(at)gmail(dot)com>, Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-03-06 13:51:02
Message-ID: f3ebe822-5a9b-42bf-bb82-93b40277f81b@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


On 2025-03-05 We 7:10 PM, Ian Lawrence Barwick wrote:
> Hi
>
> 2025年3月1日(土) 2:58 Florents Tselai <florents(dot)tselai(at)gmail(dot)com>:
>> Please add this to the next Commitfest at https://commitfest.postgresql.org/52/
>>
>>
>> Added ; thanks
>> https://commitfest.postgresql.org/patch/5260/
> I see this was committed, but there's a small formatting error in the docs
> (extra comma in the parameter list); patch attached.
>

Thanks, pushed.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com


From: "Shinoda, Noriyoshi (SXD Japan FSI)" <noriyoshi(dot)shinoda(at)hpe(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>, Ian Lawrence Barwick <barwick(at)gmail(dot)com>, Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: RE: jsonb_strip_nulls with arrays?
Date: 2025-03-06 14:17:44
Message-ID: DM4PR84MB17343BC3605930E0D98FE185EECA2@DM4PR84MB1734.NAMPRD84.PROD.OUTLOOK.COM
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi,
Thanks for developing the good feature.
I've attached a small patch for the documentation of the json_strip_nulls function. The data type of the 'target' parameter is different between the implementation and the documentation. The implementation is json_stripe_nulls (target JSON, ...), but the current documentation says json_stripe_nulls(target JSONB, ...).

Regards,
Noriyoshi Shinoda

-----Original Message-----
From: Andrew Dunstan <andrew(at)dunslane(dot)net>
Sent: Thursday, March 6, 2025 10:51 PM
To: Ian Lawrence Barwick <barwick(at)gmail(dot)com>; Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?

On 2025-03-05 We 7:10 PM, Ian Lawrence Barwick wrote:
> Hi
>
> 2025年3月1日(土) 2:58 Florents Tselai <florents(dot)tselai(at)gmail(dot)com>:
>> Please add this to the next Commitfest at
>> https://urldefense.com/v3/__https://commitfest.postgresql.org/52/__;!
>> !NpxR!moyYqgSNJn8nLYGjlKKZDuERAzIwnFY8Ge_C5MHDuoPRpTJI9Ee0gsyF4IeybO-
>> t--xu2idPkLF240-sxqpR$
>>
>>
>> Added ; thanks
>>
>> https://urldefense.com/v3/__https://commitfest.postgresql.org/patch/5
>> 260/__;!!NpxR!moyYqgSNJn8nLYGjlKKZDuERAzIwnFY8Ge_C5MHDuoPRpTJI9Ee0gsy
>> F4IeybO-t--xu2idPkLF24zKVAj03$
> I see this was committed, but there's a small formatting error in the
> docs (extra comma in the parameter list); patch attached.
>

Thanks, pushed.

cheers

andrew

--
Andrew Dunstan
EDB: https://urldefense.com/v3/__https://www.enterprisedb.com__;!!NpxR!moyYqgSNJn8nLYGjlKKZDuERAzIwnFY8Ge_C5MHDuoPRpTJI9Ee0gsyF4IeybO-t--xu2idPkLF24-WSxzSS$

Attachment Content-Type Size
json_strip_nulls_doc_v1.diff application/octet-stream 774 bytes

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: "Shinoda, Noriyoshi (SXD Japan FSI)" <noriyoshi(dot)shinoda(at)hpe(dot)com>, Ian Lawrence Barwick <barwick(at)gmail(dot)com>, Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonb_strip_nulls with arrays?
Date: 2025-03-06 15:57:32
Message-ID: 6e58dc0e-b621-4350-a1c7-77aa52366a06@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers


On 2025-03-06 Th 9:17 AM, Shinoda, Noriyoshi (SXD Japan FSI) wrote:
> Hi,
> Thanks for developing the good feature.
> I've attached a small patch for the documentation of the json_strip_nulls function. The data type of the 'target' parameter is different between the implementation and the documentation. The implementation is json_stripe_nulls (target JSON, ...), but the current documentation says json_stripe_nulls(target JSONB, ...).
>

Argh! My glasses must have been fogged up yesterday.

pushed, thanks

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com