Re: Getting json-value as varchar - Mailing list pgsql-general

From Thomas Kellerer
Subject Re: Getting json-value as varchar
Date
Msg-id f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net
Whole thread Raw
In response to Getting json-value as varchar  (Andreas Joseph Krogh <andreas@visena.com>)
List pgsql-general
Andreas Joseph Krogh schrieb am 06.01.2022 um 13:28:
> Hi, in PG-14 this query returns "value" (with double-quotes):
> SELECT ('{"key":"value"}'::jsonb)['key'];
> ┌─────────┐
> │  jsonb  │
> ├─────────┤
> │ "value" │
> └─────────┘
> (1 row)
> 
> and this returns 'value' (without the quotes):
> SELECT ('{"key":"value"}'::jsonb)->> 'key';
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value    │
> └──────────┘
> (1 row)
> 

Unfortunately there isn't a direct cast from jsonb to text, but the #>> operator can be (mis)used for this:

     SELECT ('{"key":"value"}'::jsonb)['key'] #>> '{}'


you can put that into a function if you need that frequently.




pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: pg_dump parameter
Next
From: Tom Lane
Date:
Subject: Re: md5 issues Postgres14 on OL7