Iam trying to append a dictionary in one json file to a dictionary in another json file. here are json file r1
JavaScript
x
11
11
1
[
2
{
3
"Address": "Mumbai",
4
"Email_ID": "sumit@gmail.com",
5
"EmpCode": 1,
6
"EmpName": "sumit",
7
"Id": 1,
8
"Phone_No": "7543668309"
9
}
10
]
11
json file r2
JavaScript
1
10
10
1
[
2
{
3
"Basic": 20000.0,
4
"DA": 30000.0,
5
"EmpCode": 1,
6
"Gross": 50000.0,
7
"S_ID": 1
8
}
9
]
10
The result Iam looking for is Expected result
JavaScript
1
16
16
1
[
2
{
3
"Address": "Mumbai",
4
"Email_ID": "sumit@gmail.com",
5
"EmpCode": 1,
6
"EmpName": "sumit",
7
"Id": 1,
8
"Phone_No": "7543668309"
9
"Basic": 20000.0,
10
"DA": 30000.0,
11
"EmpCode": 1,
12
"Gross": 50000.0,
13
"S_ID": 1
14
}
15
]
16
My code is not returning the expected results
JavaScript
1
11
11
1
def get_name(nid):
2
response1 = requests.get('http://10.162.14.137:5000/users/'+nid)
3
response2 = requests.request(method="GET", url='http://10.162.14.137:5001/salarylist/'+nid)
4
#return render_template('append.html', response1=response1,response2=response2)
5
r1=(response1.json())
6
r2=(response2.json())
7
r3=r1+r2
8
9
#return render_template('append.html', r3=r3)
10
return(r3)
11
Advertisement
Answer
As the ‘Adrian shum’ said your json having the lists. in that case you can use nested for loops.
JavaScript
1
6
1
result = []
2
for i, j in zip(r1, r2):
3
result.append(i | j)
4
print(result)
5
>>> [{'Address': 'Mumbai', 'Email_ID': 'sumit@gmail.com', 'EmpCode': 1, 'EmpName': 'sumit', 'Id': 1, 'Phone_No': '7543668309', 'Basic': 20000.0, 'DA': 30000.0, 'Gross': 50000.0, 'S_ID': 1}]
6