3

I am trying to replace a JSON value with a value from another JSON file. So for example I have this JSON file:

{'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}

and now I want to replace the 'street' value with a corresponding value of this JSON file:

{'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}

so the result should be:

{'data': [{'street': 'Example Street 1'}, {'street': 'Example Street 2'}, {'street': 'Example Street 3'}]}

I am trying to learn python so I don't know how to solve this problem and I couldn't find a solution on the internet. If somebody knows how to solve this please help me.

0

2 Answers 2

3

To start off with, you will need to load the contents of the first JSON file, then load the contents of the second JSON file, find the corresponding value (in this case, the 'street' key), and then dump the new JSON into the file.

In my case, first.json contained {"data": [{"street": "1"}, {"street": "2"}, {"street": "3"}]} and second.json contained {"data": [{"1": "Example Street 1"}, {"2": "Example Street 2"}, {"3": "Example Street 3"}]}

I used the following to create a new JSON file with the values specified:

import json

with open('first.json') as first_file:
    first_json = json.load(first_file)
    
with open('second.json') as second_file:
    second_json = json.load(second_file)    

new_data = []

for i in range(len(first_json['data'])):
    for j in range(len(second_json['data'])):
        key_one, value_one = list(first_json['data'][i].items())[0]
        key_two, value_two = list(second_json['data'][j].items())[0]

        if value_one == key_two:
            new_data.append({key_one: value_two})
        
with open('new_file.json', 'w+') as new_file:
    new_data = {'data': new_data}
    json.dump(new_data, new_file)

The new JSON file should contain the values

{"data": [{"street": "Example Street 1"}, {"street": "Example Street 2"}, {"street": "Example Street 3"}]}
Sign up to request clarification or add additional context in comments.

Comments

1

We take the dictA data field(It is a list) to iterate. For each element in the list We take the value of the element since the element is a dictionary and check whether it is in the dataB 'data' field list elements then we change the dataA['street'] field

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}


for elementA in dictA['data']:
  for elementB in dictB['data']:
    if elementA['street'] in elementB:
       elementA['street'] = elementB[elementA['street']]
print(dictA)

Using zip function, Iterate two list in same time

dictA ={'data': [{'street': '1'}, {'street': '2'}, {'street': '3'}]}
dictB = {'data': [{'1': 'Example Street 1'}, {'2': 'Example Street 2'}, {'3': 'Example Street 3'}]}

for elementA,elementB in zip(dictA['data'], dictB['data']):
    elementA['street'] =elementB[elementA['street']]
print(dictA)

2 Comments

That works great. Thanks. Would it also be possible to then change elementA to another element of dictB? For example: dictB = {'data': [{'1': 'Example Street 1', example_data: aa}, {'2': 'Example Street 2', example_data: bb}, {'3': 'Example Street 3', example_data: cc}]}and now change the "street" element of dictA to the corresponding "example_data" value?
You just need to change the line elementA['street'] = elementB[elementA['street']] to this one ` elementA['street'] = elementB['example_data'] `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.