Re: Getting json-value as varchar

From: Thomas Markus <t(dot)markus(at)proventis(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Getting json-value as varchar
Date: 2022-01-06 12:31:19
Message-ID: e5f0a003-e4a4-38e2-404c-56e623e40d7a@proventis.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Am 06.01.22 um 13:28 schrieb Andreas Joseph Krogh:
> 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)
>
> How to I use the subscript syntax and get the result as varchar
> instead of JSONB, assuming I /know/ the JSON-field is a String?
simply cast your value
SELECT (('{"key":"value"}'::jsonb)->> 'key')::text;

best regards
Thomas
> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas(at)visena(dot)com
> www.visena.com <https://www.visena.com>
> <https://www.visena.com>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Joseph Krogh 2022-01-06 12:36:28 Re: Getting json-value as varchar
Previous Message Andreas Joseph Krogh 2022-01-06 12:28:26 Getting json-value as varchar