Skip to content

Instantly share code, notes, and snippets.

@mindey
Last active January 22, 2018 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindey/1914fb5e3412b8a317e56857a666344e to your computer and use it in GitHub Desktop.
Save mindey/1914fb5e3412b8a317e56857a666344e to your computer and use it in GitHub Desktop.
### 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