jeudi 1 juillet 2021

Is there a way to iterate through dictionary keys in an if loop in python

I have a for loop, where i iterate through json objects in a json array, and if a key in the iterated json object is named 'corporateIdentificationNumber' then i want to update the key and value with the key and value of a dictionary. In other words, if the json object contains 'corporateIdentificationNumber', then select first key and value from the dictionary and update the json object. Then next time a json object contains 'corporateIdentificationNumber', the update with the next pair of key and value of dictionary.

file = self.r.json()
thisdict = {"Test1": "test1value", "Test2": "test2value", "Test3": "test3value"}

for i in file['collection']:
    print (f'first i = {i}') ---> output: json object
    if 'corporateIdentificationNumber' in i:
        print(f'last i = {i}') ---> output: json object with corporateIdentificationNumber
        --- first time select first key and value from dict, then at next time if loop triggers, then select next key and value from dict ---
        --- update this json object like: i['name'] = this.dict(key), i['corporateIdentificationNumber'] = this.dict(corporateIdentificationNumber) ---

is this possible to do ? Hope it makes sense, otherwise i can explain further

Aucun commentaire:

Enregistrer un commentaire