-1

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

   [
      {
        "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)
  • Based on your json, `r1` and `r2` are lists instead of a dictionary. Adding such lists are going to give you a list with 2 elements. – Adrian Shum Aug 26 '22 at 06:28
  • In order to combine two dicts, you can follow https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression – Adrian Shum Aug 26 '22 at 06:32
  • The solutions from above link is throwing errors TypeError: unsupported operand type(s) for |: 'list' and 'list' – Aparna Jins Aug 26 '22 at 06:49
  • Please read carefully what I wrote: your r1 and r2 are NOT dict. It is a list containing a dict. – Adrian Shum Aug 26 '22 at 06:56

2 Answers2

0

Your r1 and r2 are NOT dicts. They are lists having single element of dict.

Not sure what you are trying to achieve but

# Python 3.9+
r3 = [ r1[0] | r2[0] ]

# Prior to 3.9
r3 = [ { **(r1[0]), **(r2[0]) } ]

should work

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
-1

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

result = []
for i, j in zip(r1, 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}]
Ramesh
  • 635
  • 2
  • 15
  • 1
    nested loop is probably a wrong choice here. It did nothing for 1-element list, and is giving u nasty result if the lists contain more than 1 elements – Adrian Shum Aug 26 '22 at 06:56
  • 1
    comment after update: first it is syntactically wrong without indentation in 2nd loop. And, if you just want to pair up elements in 2 lists, the way you are doing is very inefficient and hard to read. Use `zip` instead – Adrian Shum Aug 26 '22 at 07:09
  • thanks for notifying indentation error. – Ramesh Aug 26 '22 at 07:12
  • Please see answer from @AdrianShum (and correction in comments) for the right way to do this – DarkKnight Aug 26 '22 at 07:15
  • @Vlad i have updated the code.please correct me in case of any errors or better way do it :) – Ramesh Aug 26 '22 at 07:28
  • 2
    You don't need an explicit loop when you can use a list comprehension like: *r3 = [x|y for x, y in zip(r1, r2)]* – DarkKnight Aug 26 '22 at 07:43