0

The goal is to update a json object that contains a particular key

The json file looks like:

{
 "collection": [
{"name": "name1", "phone": "10203040"}, 
{"name": "name2", "phone": "20304050", "corporateIdentificationNumber": "1234"}, 
{"name": "name3", "phone": "30405060", "corporateIdentificationNumber": "5678"}
]}

if a json object contains the key 'corporateIdentificationNumber', then iterate through a dictonary and update 'name' and 'corporateIdentificationNumber' from dictionary. Dictionary looks like this:

dict = {"westbuilt": "4232", "Northbound": "5556"}

In other words that means that i need to update the json object with a dictionary, and whenever i am updating a json object, it should select key/value pair from dictionary, and then iterate to next key/value for next json object containing 'corporateIdentificationNumber'

Code:

r = requests.get(url="*URL*")
file = r.json()

for i in file['collection']:
    if 'corporateIdentificationNumber' in i:
        --- select next iterated key/value from dict---
        --- update json object ---

result should look like:

   {
 "collection": [
{"name": "name1", "phone": "10203040"}, 
{"name": "westbuilt", "phone": "20304050", "corporateIdentificationNumber": "4232"}, 
{"name": "Northbound", "phone": "30405060", "corporateIdentificationNumber": "5556"}
]}
5
  • 1
    What should the resulting json file look like? Commented Jul 1, 2021 at 13:27
  • @quamrana HI, thank you for your time. I have just updated my question with how i expect the result to look like Commented Jul 1, 2021 at 13:30
  • So, you are relying on the order of items in dict (badly named btw) to overwrite values in the collection list? Commented Jul 1, 2021 at 13:32
  • Yes, exactly! Not sure, if this is the correct way of doing it. i am in deep water right now Commented Jul 1, 2021 at 13:33
  • JSON object is a dict. Commented Jul 1, 2021 at 13:34

2 Answers 2

2

I think you need to use an iterator to the items:

updates = {"westbuilt": "4232", "Northbound": "5556"}

r = requests.get(url="*URL*")
file = r.json()

items = iter(updates.items())
try:
    for i in file['collection']:
        if 'corporateIdentificationNumber' in i:
            d = next(items)
            i['name'] = d[0]
            i["corporateIdentificationNumber"] = d[1]
except StopIteration:
    pass

print(file)
Sign up to request clarification or add additional context in comments.

1 Comment

You are a life saver. This is works and is exactly what i needed. Thank you very much!
0
json_object["corporateIdentificationNumber"] = "updated value"
file = open("your_json_file.json", "w")
json.dump(json_object, file)
file.close()

2 Comments

Code only answers are not in general helpful.
The required pieces of information are integrated into the code. "updated value" and "your_json_file".

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.