Re: inconsistent jsonb 'null' conversions

From: Sehrope Sarkuni <sehrope(at)jackdb(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: inconsistent jsonb 'null' conversions
Date: 2026-07-05 15:38:18
Message-ID: CAH7T-apJSU3yxZ_VjsywLNH8EfgCYynmrEnz=GuFpHEHy1OVRw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Jul 5, 2026 at 11:13 AM Sehrope Sarkuni <sehrope(at)jackdb(dot)com> wrote:
> On Sun, Jul 5, 2026 at 8:59 AM Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> >
> > I found inconsistency in casting the value "null" of jsonb type to scalar types.
> >
> > Is it expected behaviour?
>
> I'm not sure if this is expected, but the cast from json to int not
> giving an error for null is a change from v17. That cast previously
> failed.

Thinking about this a bit more, it does make sense and I'd say its
expected behavior.

The cast from jsonb (or json) to text is not extracting the one value.
It's representing the entirety of any json/jsonb and would allow round
tripping back again. It's effectively JSON.stringify(...) of the
value. Otherwise you would not be able to represent null vs "null".

The cast to int is for a specific scalar value and only works when the
value itself is a number. It does not allow for transforming JSON
strings, just null and numbers:

SELECT '"123"'::jsonb::int
ERROR: cannot cast jsonb string to type integer

SELECT '123'::jsonb::int
int4
------
123
(1 row)

SELECT 'null'::jsonb::int IS NULL;
?column?
----------
t
(1 row)

So I'd say it's correct because the null int is the proper value to
match the null jsonb scalar.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2026-07-05 15:46:10 Re: inconsistent jsonb 'null' conversions
Previous Message Pavel Stehule 2026-07-05 15:36:18 Re: POC: PLpgSQL FOREACH IN JSON ARRAY