Re: Should we support casting between ARRAYs and JSON(B)? - Mailing list pgsql-hackers

From Marcos Pegoraro
Subject Re: Should we support casting between ARRAYs and JSON(B)?
Date
Msg-id CAB-JLwbL45nMSA+xZkEac9+BGwyfbBeittQcnU0GKPD6DbShOg@mail.gmail.com
Whole thread Raw
In response to Should we support casting between ARRAYs and JSON(B)?  (Aleksander Alekseev <aleksander@timescale.com>)
List pgsql-hackers
Em seg., 28 de out. de 2024 às 14:06, Aleksander Alekseev <aleksander@timescale.com> escreveu:
=# select '[1,2,3]' :: jsonb :: int[];

I think would be useful, cast int[] to json is not hard

select to_json('{1,5,9,12}'::int[]);

but json array to int[] is not that easy.

select js,
       js->'items',
       translate(js->>'items','[]','{}')::int[],
       5 = any(translate(js->>'items','[]','{}')::int[])

       --This one would be cool, doesn't need translate or any other trick
       --5 = any(js->'items'::int[])

from (select jsonb_build_object('items','{1,5,9,12}'::int[])) x(js);

So, if you cast one way, you can do both ways.

regards
Marcos


pgsql-hackers by date:

Previous
From: Jelte Fennema-Nio
Date:
Subject: Re: protocol-level wait-for-LSN
Next
From: Bernd Helmle
Date:
Subject: Re: [PATCH] Add sortsupport for range types and btree_gist