MySQL Forums
Forum List  »  Newbie

Merging JSONs to a single aggregated JSON with sums
Posted by: Israel Elmekies
Date: October 01, 2020 06:39AM

I have a table that contains a JSON column in it, say for example

CREATE TABLE test(jsonValue JSON)

And I have multiple values int it:

INSERT INTO test('{"x": {"values": {"a": 1, "b": 12, "c": 13}}}');
INSERT INTO test('{"x": {"values": {"a": 12, "b": 3}}}');
INSERT INTO test('{"x": {"values": {"a": 2, "c": 1}}}');
INSERT INTO test('{"x": {"values": {"a": 13}}}');

I would like to return a result where I merge all inner JSONs to a single one with the sum of the values in each JSON. So Result should be

{
"x": {
"values": {
"a": 28,
"b": 15,
"c": 14
}
}
}



How can I do this? I have little SQL knowledge and I can't seem to figure our how to write this query.
I think I need to extract and aggregate the keys to a single result => [a, b, c]
and than iterate over each key, summing values from all jsons.
I'm new to SQL and don't exactly know how to do this.

Any help will be very appreciated!

Options: ReplyQuote


Subject
Written By
Posted
Merging JSONs to a single aggregated JSON with sums
October 01, 2020 06:39AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.