Attila Kling
Attila Kling

Reputation: 1787

JSON array column into JSON object

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

Answers (1)

klin
klin

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

Related Questions