pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: How to convert return values from JSON Path functions to text
Date: Wed, 6 Nov 2019 21:51:24 +0100
Message-ID: <83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net> (raw)

The new JSON path functions in Postgres 12 are really convenient, however I cannot figure out how to properly convert their return values to a text value.

E.g. select jsonb_path_query_first('{"a": "foo"}', '$.a') returns a JSONB value.
Casting it to text, still yields "foo" (with quotes), rather than foo (without quotes)

For the time being I am using something like this:

     create function jsonb_to_text(p_value jsonb)
       returns text
     as
     $$
       select case jsonb_typeof(p_value)
                when 'string' then trim('"' from p_value::text)
                else p_value::text
              end;
     $$
     language sql
     immutable
     strict;

But that feels a bit "heavyweight" - I was hoping for an easier (and more efficient) way to do that.

Thomas







Message-ID: <83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net>
Permalink:  ../83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net/
Also on:    postgresql.org/message-id/83b22dc2-39ee-7a24-5c01-a2d29fa9da44@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: spam_eater@gmx.net
  Subject: Re: How to convert return values from JSON Path functions to text
  In-Reply-To: <83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net>
  References: <83b22dc2-39ee-7a24-5c01-a2d29fa9da44@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