Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JessicaWachtel/b857f077c8a72bc19c249e182199fca4 to your computer and use it in GitHub Desktop.
Save JessicaWachtel/b857f077c8a72bc19c249e182199fca4 to your computer and use it in GitHub Desktop.
def traverse_json(obj, indent=0):
# check if the object is a dictionary
if isinstance(obj, dict):
for key, value in obj.items():
print(' ' * indent + f"{key}:")
loop_through_json(value, indent + 4)
# check if the object is a list (not used in this example, but useful in the real-world)
elif isinstance(obj, list):
for index, item in enumerate(obj):
print(' ' * indent + f"[{index}]:")
loop_through_json(item, indent + 4)
# print the value if neither a list or dictionary are found
else:
print(' ' * indent + str(obj))
# traverse the parsed JSON data
traverse_json(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment