pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Markus <t.markus@proventis.net>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Getting json-value as varchar
Date: Thu, 6 Jan 2022 14:25:16 +0100
Message-ID: <fea89b01-5429-4328-c705-5bcd41a91322@proventis.net> (raw)
In-Reply-To: <VisenaEmail.32.ec0b963b4e92d37.17e2f6228cb@tc7-visena>
References: <VisenaEmail.2e.372da85b7b7aed09.17e2f59771e@tc7-visena>
	<e5f0a003-e4a4-38e2-404c-56e623e40d7a@proventis.net>
	<VisenaEmail.32.ec0b963b4e92d37.17e2f6228cb@tc7-visena>

Hi,

Am 06.01.22 um 13:36 schrieb Andreas Joseph Krogh:
> På torsdag 06. januar 2022 kl. 13:31:19, skrev Thomas Markus 
> <t.markus@proventis.net>:
>
>     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
>     Thoma
>
> I think you misread my message. What I want is for the subscript-version:
>> ('{"key":"value"}'::jsonb)['key']
> to return:
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value    │
> └──────────┘
>
> instead of
> ┌─────────┐
> │  jsonb  │
> ├─────────┤
> │ "value" │
> └─────────┘
yeah right :(

complicated but this should do:
SELECT  jsonb_build_array( ('{"key":"value"}'::jsonb)['key'] ) ->> 0;



> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com <https://www.visena.com;
> <https://www.visena.com;

Attachments:

UCRjWK99lWlgoYmK.png (image/png, 1.9K) download view image

view thread (11+ messages)  latest in thread

Message-ID: <fea89b01-5429-4328-c705-5bcd41a91322@proventis.net>
Permalink:  ../fea89b01-5429-4328-c705-5bcd41a91322@proventis.net/
Also on:    postgresql.org/message-id/fea89b01-5429-4328-c705-5bcd41a91322@proventis.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: t.markus@proventis.net
  Subject: Re: Getting json-value as varchar
  In-Reply-To: <fea89b01-5429-4328-c705-5bcd41a91322@proventis.net>
  References: <VisenaEmail.2e.372da85b7b7aed09.17e2f59771e@tc7-visena> <e5f0a003-e4a4-38e2-404c-56e623e40d7a@proventis.net> <VisenaEmail.32.ec0b963b4e92d37.17e2f6228cb@tc7-visena> <fea89b01-5429-4328-c705-5bcd41a91322@proventis.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox