-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,709
  • 16
  • 69
  • 112

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
  • 3,280
  • 3
  • 15
  • 23
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
  • 159,742
  • 34
  • 281
  • 339