Json_Final =[]
try:
for keyIN in JSON1:
json_data_Merge= {}
for keyUS in JSON2:
if(keyIN['YEAR'] == keyUS['YEAR']) & (keyIN['MONTH'] == keyUS['MONTH'])& (keyIN['Name'] == keyUS['Name']):
json_data_Merge['YEAR'] = keyIN['YEAR']
json_data_Merge['MONTH'] = keyIN['MONTH']
json_data_Merge['Name'] = keyIN['Name']
json_data_Merge['Total']= int(keyIN['Total']) + int(keyUS['Total'])
Json_Final.append(json_data_Merge)
print( Json_Final )
except Exception as e:
print('MergeException',e)
JSON 1 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 100},
{"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 200},
{"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300},
{"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total": 100},
{"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total": 200}]
JSON 2 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 200},
{"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300},
{"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 400},
{"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300},
{"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200},
{"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total": 500},
{"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200},
{"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total": 250}]
Expected Output:
[{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 300},
{"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300},
{"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 600},
{"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300},
{"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300},
{"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200},
{"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total": 600},
{"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200},
{"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total": 450}]