Created
January 13, 2025 15:09
-
-
Save Magnuti/f1978b73338e8dc0995ab9a63fd4747b to your computer and use it in GitHub Desktop.
Iterate JSON structure in Python
This file contains hidden or 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 iterate_dict(d, path="", level=0): | |
for key, value in d.items(): | |
new_path = f"{path}.{key}" if path else key | |
if isinstance(value, dict): | |
yield new_path | |
yield from iterate_dict(value, new_path, level+1) | |
else: | |
yield new_path | |
with open(filename, 'r') as f: | |
some_dict = json.load(f) | |
for line in iterate_dict(some_dict): | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment