Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map a jsonb array of numbers to a jsonb array of strings?

I have a jsonb array of numbers or strings, e.g. '[1, "2", 3]'::jsonb

I want to map all values to strings, so that I can end up with '["1", "2", "3"]'::jsonb

I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select jsonb_agg(jsonb_array_elements_text('[1, 2, 3]'::jsonb))

But it is complaining

LINE 1: select jsonb_agg(jsonb_array_elements_text('[1, 2, 3]'::json...
                         ^
HINT:  You might be able to move the set-returning function into a LATERAL FROM item.

>Solution :

Do what the error message suggests: use the set returning function jsonb_array_elements_text() like a table:

select jsonb_agg(element)
from jsonb_array_elements_text('[1, 2, 3]'::jsonb) as x(element);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading