Skip to content

Instantly share code, notes, and snippets.

@Bjwebb
Last active February 15, 2019 05:35
Show Gist options
  • Save Bjwebb/d86341888d4cab5b8cfb2f35c927ad8a to your computer and use it in GitHub Desktop.
Save Bjwebb/d86341888d4cab5b8cfb2f35c927ad8a to your computer and use it in GitHub Desktop.
List the schema level properties used in a JSON schema
import os
import json
import jsonref
# From libcove/lib/common.py
class CustomJsonrefLoader(jsonref.JsonLoader):
'''This ref loader is only for use with the jsonref library
and NOT jsonschema.'''
def get_remote_json(self, uri, **kwargs):
if uri[:4] == 'http':
return super().get_remote_json(uri, **kwargs)
else:
with open(uri) as schema_file:
return json.load(schema_file, **kwargs)
loader = CustomJsonrefLoader()
def collect_properties(subschema, parent_key=''):
properties = set()
for key, val in subschema.items():
if isinstance(val, dict):
properties.update(collect_properties(val, key))
elif isinstance(val, list):
for x in val:
if isinstance(x, dict):
properties.update(collect_properties(x))
elif isinstance(x, str):
continue
else:
raise NotImplementedError()
if parent_key != 'properties':
properties.update(subschema.keys())
return properties
os.chdir('schema')
schema = jsonref.load(open('bods-package.json'), loader=loader)
for x in sorted(collect_properties(schema)):
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment