On Tue, Sep 17, 2024 at 5:11 PM Andrew Dunstan <and...@dunslane.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 > >
v1-0002-Add-docs-for-strip_in_arrays-argument.patch
Description: Binary data
v1-0001-jsonb_strip_nulls-jsonb-bool-wip.patch
Description: Binary data