I have an array of strings, like this:
SELECT ARRAY['[email protected]', '[email protected]', '[email protected]'];How do I convert (map?) it into a jsonb array of jsonb objects like this?:
SELECT [{"email": "[email protected]"}, {"email": "[email protected]"}, {"email": "[email protected]"}]::jsonb;1 Answer1
SELECT jsonb_agg(jsonb_build_object('email', elems))FROM ( SELECT ARRAY['[email protected]', '[email protected]', '[email protected]'] AS a) s,unnest(a) AS elems- Expand the array elements into one record each with
unnest() - Create the JSON objects using
jsonb_object_build()to create the key/value structure your are expecting - re-aggregate these objects into one new JSON array using
jsonb_agg()
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
