Re: how to correctly cast json value to text? - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: how to correctly cast json value to text?
Date
Msg-id CAFj8pRBDwnvr0Ars292jK44Y+6_nPySvNBAg=WAVCWZOOpmW0g@mail.gmail.com
Whole thread Raw
In response to Re: how to correctly cast json value to text?  (Marko Tiikkaja <marko@joh.to>)
List pgsql-hackers


po 3. 5. 2021 v 11:26 odesílatel Marko Tiikkaja <marko@joh.to> napsal:
On Mon, May 3, 2021 at 12:24 PM Pavel Stehule <pavel.stehule@gmail.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

pgsql-hackers by date:

Previous
From: Marko Tiikkaja
Date:
Subject: Re: how to correctly cast json value to text?
Next
From: Peter Eisentraut
Date:
Subject: Re: strange error reporting