I have this rows of json like so:
1. {"0": 2}
2. {"2": 0}
3. {"3": 0}
4. {"4": 1}
5. {"13": 0}
How to convert it into a single object?
{
"0": 2,
"2": 0,
"3": 0,
"4": 1,
"13": 0
}
I tried experimenting with jsonb_agg but not working
I have this rows of json like so:
1. {"0": 2}
2. {"2": 0}
3. {"3": 0}
4. {"4": 1}
5. {"13": 0}
How to convert it into a single object?
{
"0": 2,
"2": 0,
"3": 0,
"4": 1,
"13": 0
}
I tried experimenting with jsonb_agg but not working
2
Answers
I think the easiest is to define an aggregate that does this:
Then you can do:
You can do it using
jsonb_each
to split object into a set of key/value pairs. andjsonb_object_agg
to returns the aggregated key–value pairs as a jsonb object :Demo here