Reputation: 1787
Is it possible to turn a json array (["a","b","c"]
) column into a json object ({"a":"a", "b":"b", "c":"c"}
) in postgresql?
Upvotes: 0
Views: 98
Reputation: 121889
Unnest the array with jsonb_array_elements_text()
and aggregate the elements back using jsonb_object_agg()
:
select jsonb_object_agg(value, value)
from jsonb_array_elements_text('["a","b","c"]'::jsonb);
jsonb_object_agg
--------------------------------
{"a": "a", "b": "b", "c": "c"}
(1 row)
Upvotes: 2