-1

I have a json like this:

{
"name": "ehsan",
"family": "shirzadi",
"age": 20,
"address": "...",
"tel": "..."
}

I have another json like this:

{
"name": "ali",
"family": "rezayee",
}

Is there away to update my first json's name and family with the second json without using a loop and assigning one by one?

ehsan shirzadi
  • 4,448
  • 15
  • 61
  • 108

2 Answers2

3

If we assume the first json is j1 and the second j2 then the following will update j1 with values in j2:

j1.update(j2)
Ron Kalian
  • 2,754
  • 2
  • 14
  • 22
1

You can import / export json files to / from dictionaries. This means you can utilise dict.update:

d1 = {
"name": "ehsan",
"family": "shirzadi",
"age": 20,
"address": "...",
}

d2 = {
"name": "ali",
"family": "rezayee",
}

d1.update(d2)

print(d1)

{'name': 'ali', 'family': 'rezayee',
 'age': 20, 'address': '...'}
jpp
  • 147,904
  • 31
  • 244
  • 302