Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oylenshpeegul
Created March 17, 2013 21:29
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 oylenshpeegul/5183735 to your computer and use it in GitHub Desktop.
Save oylenshpeegul/5183735 to your computer and use it in GitHub Desktop.
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