I have a JSON with many nested dictionaries and list that I need to modify and when I want to find the data it returns no result. However it prints the expected values before the return.
Below a simplified structure of the JSON that I need to work with:
nested_dict = {'k': {'component':'media','value':'It is a test'}}
My code below:
def find_component(data: object, component: str):
"""
"""
if isinstance(data, list):
for i, k in enumerate(data):
find_component(data[i], component)
if isinstance(data, dict):
for k, v in data.items():
if k == 'component' and v == component:
print(k, v)
print('Find Component', data)
return data
else:
find_component(data[k], component)
# Call to the recursive function
res = find_component(nested_dict, 'media')
The results of the print are the expected ones:
component media
Find Component {'component': 'media', 'value': 'It is a test'}
However the result of the data is None