Re: how to correctly cast json value to text?

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: how to correctly cast json value to text?
Date: 2021-05-03 09:35:34
Message-ID: CAFj8pRBDwnvr0Ars292jK44Y+6_nPySvNBAg=WAVCWZOOpmW0g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

po 3. 5. 2021 v 11:26 odesílatel Marko Tiikkaja <marko(at)joh(dot)to> napsal:

> On Mon, May 3, 2021 at 12:24 PM Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
> wrote:
>
>> Is it possible to do this with built functionality?
>>
>> I miss the cast function for json scalar string value to string.
>>
>
> #>>'{}'
>

It is working. Thank you. But this syntax is a little bit scary. Maybe we
can introduce some functions for this case. Until to pg 14 this
functionality was not necessary, but now it can be nice to have it.

DO $$
DECLARE v jsonb;
BEGIN
-- hodnota musi byt validni json
v['a'] = '"Ahoj"';
RAISE NOTICE '%', v['a'] #>> '{}';
END;
$$;
NOTICE: Ahoj
DO

Some ideas about the name of this function?

CREATE OR REPLACE FUNCTION jsonscalar_to_text(jsonb)
RETURNS text AS $$
SELECT $1 #>> '{}'
$$ LANGUAGE sql;

>
> .m
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2021-05-03 10:08:04 Re: strange error reporting
Previous Message Marko Tiikkaja 2021-05-03 09:25:59 Re: how to correctly cast json value to text?