How to convert return values from JSON Path functions to text - Mailing list pgsql-general

From Thomas Kellerer
Subject How to convert return values from JSON Path functions to text
Date
Msg-id 83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net
Whole thread Raw
List pgsql-general
The new JSON path functions in Postgres 12 are really convenient, however I cannot figure out how to properly convert
theirreturn 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





pgsql-general by date:

Previous
From: Peter
Date:
Subject: Re: Locked out of schema public (pg_dump lacks backup of the grant)
Next
From: Tom Lane
Date:
Subject: Re: Locked out of schema public (pg_dump lacks backup of the grant)