Check a JSON file against a Schema.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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