Last active
March 22, 2021 20:13
-
-
Save avalanchy/93b86ede01e3b294d5c1c143f4ff93fc to your computer and use it in GitHub Desktop.
python dict list schema
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
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