Reputation: 39
I have a recursive traversal function to go through a JSON object and return the information that I want. The problem is that it's not returning anything. I know that the recursion is working properly because I modified to function to print out the input at each step and it was printing out the expected results - including the final step.
def wikipedia_JSON_traversal(wiki):
if type(wiki)==dict:
if 'definitions' in wiki.keys():
wikipedia_JSON_traversal(wiki['definitions'])
elif 'text' in wiki.keys():
wikipedia_JSON_traversal(wiki['text'])
else:
pass
elif type(wiki)==list:
wikipedia_JSON_traversal(wiki[0])
else:
return wiki
Upvotes: 0
Views: 65
Reputation: 2750
You need a return after each function call, the returns don't bubble up automatically.
def wikipedia_JSON_traversal(wiki):
if type(wiki)==dict:
if 'definitions' in wiki.keys():
return wikipedia_JSON_traversal(wiki['definitions'])
elif 'text' in wiki.keys():
return wikipedia_JSON_traversal(wiki['text'])
else:
pass
elif type(wiki)==list:
return wikipedia_JSON_traversal(wiki[0])
else:
return wiki
Upvotes: 1