I've got a number of JSON files of different structures that all have a number of levels of "nesting". I need to examine certain keys that can appear anywhere in the object and update these.
Am struggling to handle the "nested" elements
I've got some code that have cobbled together from much Googling (am new to Python) and have tried both instance(value,dict) and type(value) is dict
However in no cases has the log showed "iterating deeper".
def iterate(dictionary):
for key, value in dictionary.items():
print('key {} -> value {}'.format(key, value))
#if logic to update specific values
print('Dict new value {}'.format(value))
if isinstance(value, dict):
#if type(value) is dict:
print('Iterating deeper. Key {}'.format(key))
dictionary[key] = iterate(value)
return(dictionary)
Simple JSON example:
myObj = {
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
I'd expect to see "Iterating deeper. Key cars" printed out
elifwithoutif?