Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Check a JSON file against a Schema.
#!/usr/bin/env python
import argparse
import json
import jsonschema
parser = argparse.ArgumentParser(
description='validate the json file against the json schema')
parser.add_argument(
'datafile', type=argparse.FileType('r'),
help='the file containing the json data to be validated')
parser.add_argument(
'--schemafile', type=argparse.FileType('r'),
help='the file containing the json schema to validate against')
args = parser.parse_args()
data = json.load(args.datafile)
print("{} is syntactically valid".format(args.datafile.name))
if args.schemafile is not None:
schema = json.load(args.schemafile)
print("{} is syntactically valid".format(args.schemafile.name))
jsonschema.validate(data, schema)
print("{} is semantically valid".format(args.datafile.name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment