Search Postgresql Archives

How to convert return values from JSON Path functions to text

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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







[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux