Re: Why does jsonb_set() remove non-mentioned keys?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Thomas Kellerer <spam_eater(at)gmx(dot)net>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Why does jsonb_set() remove non-mentioned keys?
Date: 2019-07-04 16:20:56
Message-ID: CAKFQuwaEErw0+DH6w+XwzbSMYiWdcRO5Bcy2ts_1C6FTB0TxMw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jul 4, 2019 at 6:18 AM Thomas Kellerer <spam_eater(at)gmx(dot)net> wrote:

> Why does
>
> select jsonb_set('{"foo": 1}'::jsonb, '{bar}', to_jsonb(null::int),
> true)
>
> return NULL when all it should do is to add a second key?
>

Both functions involved are defined as being STRICT (null on null input).
You need to COALSESCE SQL NULL to JSON 'null' to obtain the result you
desire. This is a general truth when dealing with the JSON type in
PostgreSQL.

select jsonb_set('{"foo": 1}'::jsonb, '{bar}',
coalesce(to_jsonb(null::int), 'null'), true)

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben Snaidero 2019-07-04 16:37:06 Re: pg_dump (COPY) hanging intermittently
Previous Message Adrian Klaver 2019-07-04 14:58:22 Re: PostgreSQL upgrade from 9.4.2 to 9.6.12