Skip to content

Instantly share code, notes, and snippets.

@dbrant
Last active January 4, 2024 17:52
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 dbrant/f260502106f9d3c9bae98cabfa29e8c2 to your computer and use it in GitHub Desktop.
Save dbrant/f260502106f9d3c9bae98cabfa29e8c2 to your computer and use it in GitHub Desktop.
Validate JSON from a URL against a Json Schema from another URL.
# Make sure to install 'jsonschema' as well as any validator packages,
# such as 'rfc3339-validator', that are used by the schema.
import json
import sys
import urllib.request
import urllib.error
import jsonschema
def main():
if len(sys.argv) < 3:
print('Usage: validate_json.py <json_file_url> <json_schema_url>', file=sys.stderr)
sys.exit(1)
with urllib.request.urlopen(sys.argv[1]) as json_file:
json_data = json_file.read().decode()
with urllib.request.urlopen(sys.argv[2]) as json_schema_file:
json_schema = json_schema_file.read().decode()
jsonschema.validate(json.loads(json_data), json.loads(json_schema), format_checker=jsonschema.FormatChecker())
print("Validation successful!")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment