Last active
January 22, 2018 09:43
-
-
Save mindey/1914fb5e3412b8a317e56857a666344e 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
### For finding schema | |
def schematize(obj): | |
''' | |
Get schema of nested JSON, assuming first item in lists. | |
''' | |
if isinstance(obj, dict): | |
return {k: schematize(v) for k, v in obj.items()} | |
elif isinstance(obj, list): | |
return [schematize(elem) for elem in obj][:1] | |
else: | |
return type(obj) # no container, just values (str, int, float) | |
### // | |
# EXAMPLES: | |
# How do we call this? literal-schema ? | |
# | |
# Data: | |
# {'hello': 'world'} | |
# | |
# Schema: | |
# {'hello': 'STRING'} | |
# | |
# Data: | |
# | |
# {'hello': {'new': 'world'}} | |
# | |
# Schema: | |
# | |
# {'hello': {'new': 'STRING'}} | |
# | |
# | |
# Data: | |
# | |
# {'hello': ['a', 'b', 'c']} | |
# | |
# Schema: | |
# | |
# {'hello': ['STRING']} | |
# | |
# | |
# Data: | |
# | |
# {'hello': [{'new': 'world', 'index': 1}, {'new': 'great', 'index': 2}]} | |
# | |
# Schema: | |
# | |
# {'hello': [{'new': 'STRING', 'index': 'INTEGER'}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment