Skip to content

Instantly share code, notes, and snippets.

@mindey
Last active Jan 22, 2018
Embed
What would you like to do?
### 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