0

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:

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.

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works! I could tell from my research that the answer was going to look something like this, but I couldn't get the proper incantation. if you don't mind a follow-up, what is the meaning of the "x()" in your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.