Re: Getting json-value as varchar

From: Thomas Kellerer <shammat(at)gmx(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Getting json-value as varchar
Date: 2022-01-06 14:39:30
Message-ID: f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2022-01-06 16:40:04 Re: md5 issues Postgres14 on OL7
Previous Message David G. Johnston 2022-01-06 14:37:04 Re: pg_dump parameter