My goal is to merge following four Jsons into one:
'{}'
'{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}'
'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
'{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}'
The requirements are:
- It shall be written in Python 3.
- Dictionary to far right: only values are allowed to be overwritten.
- All dictionaries keys are NOT allowed to be overwritten nor deleted. This is also the keys for dictionaries far right. Keys are not allowed to be lost when merging two jsons.
- When merging two dictionaries with same keys, then the values are appended/added.
The final result shall look like this:
{
"A (1)": {
"B (2)": {
"C (3)": [
{
"fewfesfz": 48,
"qwerty": 2
}
],
"D (3)": [
{
"blablabla": 1
}
]
},
"E (2)": {
"F (3)": [
{
"blablabla": 20
}
]
}
}
}
My solution is the following:
import json
s = json.loads('{}')
x = json.loads('{"A (1)": {"B (2)": {"C (3)": [{"fewfesfz": 48, "qwerty": 2}]}}}')
y = json.loads('{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}')
z = json.loads('{"A (1)": {"E (2)": {"F (3)": [{"blablabla": 20}]}}}')
def merge_json(json1, json2):
try:
json1['A (1)']['B (2)'].update(json2['A (1)']['B (2)'])
return json1
except KeyError:
return {**json1, **json2}
result = merge_json(x, s)
result = merge_json(result, y)
result['A (1)'].update(z['A (1)'])
print(json.dumps(x, indent=4))
As you can see, it is very hard to manage long term. Specially, if the depths gets changed from 3 to 4 nodes or from 3 to 2 nodes. There is no way of knowing, in which order the big letters (A, B, C, D...) come. So far, I can only come up with extremely complex functions with lot of if statements to test if the nodes exists or not. Only then I know, if I can make full merge or need to call the update dictionary method.
Have anyone a better solution how to solve this problem, then I would be very grateful.
EDIT 1 (Added more examples below):
If the following two json are merged
json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'
Result would be:
'{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 2}]}}}'
Only the value for the key "blablabla" changed from 1 to 2. This happens because it was processed later.
If following two json are merged:
json1 = '{"A (1)": {"B (2)": {"D (3)": [{"blablabla": 1}]}}}'
json2 = '{"A (1)": {"D (3)": [{"blablabla": 1}]}}'
Result would be:
{'A (1)': {'B (2)': {'D (3)': [{'blablabla': 1}]}, 'D (3)': [{'blablabla': 1}]}}
The input structure will remain the same in output.