Skip to content

Instantly share code, notes, and snippets.

@Magnuti
Created January 13, 2025 15:09
Show Gist options
  • Save Magnuti/f1978b73338e8dc0995ab9a63fd4747b to your computer and use it in GitHub Desktop.
Save Magnuti/f1978b73338e8dc0995ab9a63fd4747b to your computer and use it in GitHub Desktop.
Iterate JSON structure in Python
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