From: tomas@tuxteam.de
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: jsonb: unwrapping text
Date: Thu, 28 Oct 2021 10:53:58 +0200
Message-ID: <20211028085358.GB17431@tuxteam.de> (raw)
In-Reply-To: <CAKFQuwaqLrA3Bfk9QOUSBb5Xu_VFJ=j-1ZvP9xmgcf0an0sdyA@mail.gmail.com>
References: <20211027185807.GA31130@tuxteam.de>
<CAKFQuwaqLrA3Bfk9QOUSBb5Xu_VFJ=j-1ZvP9xmgcf0an0sdyA@mail.gmail.com>
On Wed, Oct 27, 2021 at 04:18:20PM -0700, David G. Johnston wrote:
> On Wed, Oct 27, 2021 at 11:58 AM <tomas@tuxteam.de> wrote:
>
> >
> > 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?
> >
> >
> Not sure if this exact behavior is trustworthy - but you are on the right
> path. Place the value into either a json array or json object and then use
> the text versions of the accessor methods to get the json value to pass
> through the decoding routine.
Thanks a bunch :)
I know that, behind the scenes, jsonb scalars (didn't check that
for json) are actually represented as one-element arrays, but was unsure
how much this can be relied on as "official interface" :-)
This leaves us with
foo=# select jsonb_build_array('"foo"'::jsonb)->>0;
?column?
----------
foo
(1 row)
...which feels somewhat roundabout, but hey, it actually works. I'll
What also seems to work is #>> with an empty path specifier, i.e.
select '"foo"'::jsonb #>> '{}';
...but all of them feel somewhat hacky. I'll post a request with the
form linked in [1], let's see :-)
Thanks again for your assessment, cheers
- t
Attachments:
signature.asc (application/pgp-signature, 197B) download
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, david.g.johnston@gmail.com
Subject: Re: jsonb: unwrapping text
In-Reply-To: <20211028085358.GB17431@tuxteam.de>
References: <20211027185807.GA31130@tuxteam.de> <CAKFQuwaqLrA3Bfk9QOUSBb5Xu_VFJ=j-1ZvP9xmgcf0an0sdyA@mail.gmail.com> <20211028085358.GB17431@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