Created
August 31, 2024 19:45
-
-
Save JessicaWachtel/b857f077c8a72bc19c249e182199fca4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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