pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: tomas@tuxteam.de
To: pgsql-general@lists.postgresql.org
Subject: jsonb: unwrapping text
Date: Wed, 27 Oct 2021 20:58:07 +0200
Message-ID: <20211027185807.GA31130@tuxteam.de> (raw)

Hi,

I'm trying to extract text from a jsonb 'string'. Simply casting
gives me the string wrapped in quotes:

  foo=# select '"foo"'::jsonb::text;
   text  
  -------
   "foo"
  (1 row)

This, of course, makes kind of sense, since it /is/ the JSON's
textual representation.

What is the canonical way to get at the string's content itself?

I've found out that one can treat a string singleton as if it
were an array:

  foo=# select '"foo"'::jsonb ->> 0;
   ?column? 
  ----------
   foo
  (1 row)

which conveniently returns the right type. My question: can I rely
on that, or am I missing a much more obvious option?

(I try to stay compatible down to PostgreSQL 11, 9 when possible.
Conservative customers and that).

Thanks for any insights, cheers
-- tomás

Attachments:

  signature.asc (application/pgp-signature, 197B) download

view thread (3+ messages)  latest in thread

Message-ID: <20211027185807.GA31130@tuxteam.de>
Permalink:  ../20211027185807.GA31130@tuxteam.de/
Also on:    postgresql.org/message-id/20211027185807.GA31130@tuxteam.de

 · 

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: tomas@tuxteam.de
  Subject: Re: jsonb: unwrapping text
  In-Reply-To: <20211027185807.GA31130@tuxteam.de>
  References: <20211027185807.GA31130@tuxteam.de>

* 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