I have two JSON objects. I want to merge them but wherever the keys are the same the field obj_count should be summed. Is there any way around it in python?
Here is an example of it:
This is the 1st JSON object
[
{"text": " pen and ink and watercolour", "id": "x32505 ", "obj_count": 1855},
{"text": " watercolour", "id": "x33202 ", "obj_count": 674},
{"text": "pencil", "id": "AAT16013 ", "obj_count": 297}
]
And here is the second json object
[
{"text": " pen and ink and watercolour", "id": "x32505 ", "obj_count": 807},
{"text": " watercolour", "id": "x33202 ", "obj_count": 97},
{"text": " ink", "id": "AAT15012 ", "obj_count": 297}
]
What I want is something like this:
[
{"text":" pen and ink and watercolour","id":"x32505 ","obj_count": 2662 #summed},
{"text":" watercolour","id":"x33202 ","obj_count": 771 #summed},
{"text":" ink","id":"AAT15012 ","obj_count":297},
{"text":"pencil","id":"AAT16013 ","obj_count":297}
]
2
Answers
Yes
Any loading/saving can be done with the json module (not used below though)
output
Use a
dict
to store whether you have seen anid
or notobj_count