Could someone help me with my code below?
This is originally meant to work with data in a json file but I have converted it to work with a json / dictionary variable.
Right now the get_data_value() function is working but instead of just returning the value, I would like to return a singular dict containing the key and value.
I am just not sure how to convert the item_generator function to make this possible without ruining the recursion; I found this function from an example here on stackoverflow.
def get_data_value(data,data_name):
d = data['test']
print(item_generator(d,data_name))
for _ in item_generator(d,data_name):
return (_)
def item_generator(json_input, lookup_key):
if isinstance(json_input, dict):
for key, value in json_input.items():
if key == lookup_key:
data_single_item = {key:value} # what i want to return
print(data_single_item)
yield value # only value is returned
else:
yield from item_generator(value, lookup_key)
elif isinstance(json_input, list):
for item in json_input:
yield from item_generator(item, lookup_key)
json_data = { "test": [ { "Tier1": [ { "Tier1-Main-Title-1": [ { "title": "main", "example": 400 } ] } ] }, { "Tier2": [] }, { "Tier3": [ { "Example1-Sub1": 44 } ] } ] }
print(get_data_value(json_data,'title'))
item_generatorfunction here? meaning, would we be required to use it?