Cast jsonb to numeric, int, float, bool

From: Anastasia Lubennikova <a(dot)lubennikova(at)postgrespro(dot)ru>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Cast jsonb to numeric, int, float, bool
Date: 2017-02-01 14:41:34
Message-ID: 0154d35a-24ae-f063-5273-9ffcdf1c7f2e@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Now the simplest way to extract booleans and numbers from json/jsonb is
to cast it
to text and then cast to the appropriate type:

postgres=# select 'true'::jsonb::text::bool;
bool
------
t
postgres=# select '1.0'::jsonb::text::numeric;
numeric
---------
1.0

This patch implements direct casts from jsonb numeric (jbvNumeric) to
numeric, int4 and float8,
and from jsonb bool (jbvBool) to bool.

postgres=# select 'true'::jsonb::bool;
bool
------
t

postgres=# select '1.0'::jsonb::numeric;
numeric
---------
1.0

Waiting for your feedback.
If you find it useful, I can also add support of json and other types,
such as smallint and bigint.

--
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

Attachment Content-Type Size
jsonb_numeric,int,float,bool_cast_v1.patch text/x-patch 5.7 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Vik Fearing 2017-02-01 15:18:03 Re: new autovacuum criterion for visible pages
Previous Message Rafia Sabih 2017-02-01 14:34:31 Re: WIP: [[Parallel] Shared] Hash