Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concatanate 2 dictionaries in 2 json files

Iam trying to append a dictionary in one json file to a dictionary in another json file.
here are json file
r1

[
  {
    "Address": "Mumbai",
    "Email_ID": "sumit@gmail.com",
    "EmpCode": 1,
    "EmpName": "sumit",
    "Id": 1,
    "Phone_No": "7543668309"
  }
]

json file r2

[
  {
    "Basic": 20000.0,
    "DA": 30000.0,
    "EmpCode": 1,
    "Gross": 50000.0,
    "S_ID": 1
  }
]

The result Iam looking for is
Expected result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   [
      {
        "Address": "Mumbai",
        "Email_ID": "sumit@gmail.com",
        "EmpCode": 1,
        "EmpName": "sumit",
        "Id": 1,
        "Phone_No": "7543668309"
        "Basic": 20000.0,
        "DA": 30000.0,
        "EmpCode": 1,
        "Gross": 50000.0,
        "S_ID": 1
      }
    ]

My code is not returning the expected results

def get_name(nid):
    response1 = requests.get('http://10.162.14.137:5000/users/'+nid)
    response2 = requests.request(method="GET", url='http://10.162.14.137:5001/salarylist/'+nid)
    #return render_template('append.html', response1=response1,response2=response2)
    r1=(response1.json())
    r2=(response2.json())
    r3=r1+r2
        
    #return render_template('append.html', r3=r3)
    return(r3)

>Solution :

As the ‘Adrian shum’ said your json having the lists. in that case you can use nested for loops.

result = []
for i in r1:
    for j in r2:
        result.append(i | j)
print(result)
>>> [{'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}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading