How to convert return values from JSON Path functions to text

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема How to convert return values from JSON Path functions to text
Дата
Msg-id 83b22dc2-39ee-7a24-5c01-a2d29fa9da44@gmx.net
обсуждение исходный текст
Список 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 по дате отправления:

Предыдущее
От: Peter
Дата:
Сообщение: Re: Locked out of schema public (pg_dump lacks backup of the grant)
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Locked out of schema public (pg_dump lacks backup of the grant)