Thread: Why is JSONB field automatically cast as TEXT?
First post here at PostgreSQL; please forgive any etiquette mistakes…
I have a query that extracts a field from a JSONB column, e.g.:
SELECT (((mytable.ajsonbcolumn -> ‘somedata’) -> ‘nested’) ->> ‘first_name’) AS fname FROM mytable
When I save it into a view, PostgreSQL transforms it thusly:
SELECT (((mytable.ajsonbcolumn -> ‘somedata’::text) -> ‘nested’::text) ->> ‘first_name’::text) AS fname FROM mytable
(note the ::text casts).
Why does it do this? It seems unnecessary and pollutes my SQL with a ton of extra text.
Thanks for your thoughts. -Ben
On Monday, September 17, 2018, Ben Uphoff <buphoff@villagemd.com> wrote:
SELECT (((mytable.ajsonbcolumn -> ‘somedata’::text) -> ‘nested’::text) ->> ‘first_name’::text) AS fname FROM mytable
It’s casting the untyped literal constants (somedata, neated, first_name) to text because everything must be typed. It is not casting the first or intermediate jsonb results to text. The final output is text because of the ->> operator.
:: binds more tightly than the other operators.
Jsonb->('somedata'::text)
David J.