been trying to figure this out for a while now, ho...
# sql
a
been trying to figure this out for a while now, how can i merge two JSON objects into one and add the values together? example:
Copy code
To insert:
{ "a": 1, "b": 2 }

Already there:
{ "a": 2, "b": 3, "c": 1}

What I want:
{ "a": 3, "b": 5, "c": 1 }
what's the best way to do this with SQL?
c
a
already tried that, it didn't add the numbers like i wanted
c
aaa, sorry - I didn't see that you want to sum the values of duplicate fields
a
no worries haha
c
I am pretty sure there is no built-in operator/function to do that - you would have to iterate over the keys and merge manually
a
yeah i was just looking into that
sad, but itll have to do haha
s
c
actually, there might be
have you looked at
Copy code
jsonb_object_agg
a
it took me way too long but i figured out an obscure way to do it convert the json into rows of keys/values change what i need convert back to json