pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Getting json-value as varchar
Date: Thu, 6 Jan 2022 15:39:30 +0100
Message-ID: <f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net> (raw)
In-Reply-To: <VisenaEmail.2e.372da85b7b7aed09.17e2f59771e@tc7-visena>
References: <VisenaEmail.2e.372da85b7b7aed09.17e2f59771e@tc7-visena>

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.





view thread (11+ messages)

Message-ID: <f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net>
Permalink:  ../f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net/
Also on:    postgresql.org/message-id/f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.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: shammat@gmx.net
  Subject: Re: Getting json-value as varchar
  In-Reply-To: <f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.net>
  References: <VisenaEmail.2e.372da85b7b7aed09.17e2f59771e@tc7-visena> <f6cb64ab-0306-d9d3-bc50-74992d650a46@gmx.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