Skip to content

Instantly share code, notes, and snippets.

@avalanchy
Last active March 22, 2021 20:13
Show Gist options
  • Save avalanchy/93b86ede01e3b294d5c1c143f4ff93fc to your computer and use it in GitHub Desktop.
Save avalanchy/93b86ede01e3b294d5c1c143f4ff93fc to your computer and use it in GitHub Desktop.
python dict list schema
import json
def schema(obj, parent="obj"):
if isinstance(obj, list):
if obj:
last = len(obj) - 1
schema(obj[last], f'{parent}[{last}]')
else:
print(f"{parent}[]")
return
if isinstance(obj, dict):
for key, val in sorted(obj.items()):
schema(val, f'{parent}["{key}"]')
return
try:
loaded = json.loads(obj)
except Exception:
pass
else:
schema(loaded, f"json.loads({parent})")
return
print(f"{parent} = {type(obj)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment