-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdlinejsonvalidator.py
65 lines (47 loc) · 2.13 KB
/
cmdlinejsonvalidator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
###################################################################################
###################### Python Script to validate JSON file ########################
###################################################################################
### Usage ###
# To run this script you must have the following:
# Python 2.7
# Python modules json and jsonschema installed on your machine.
# Simply run following command in terminal to validate json file against schema:
# ./cmdlinejsonvalidator.py example.json jsonschema.json
# Where example will be the name of your JSON file and jsonschema is the schema
# you wish to compare the json file against.
# ***NOTE***
# If you do not place the script in same directory as the jsonschema file and
# json file you will need to use absolute/relative path names to the files as
# your arguments.
###################################################################################
###################################################################################
import sys
import json
import jsonschema
from jsonschema import validate
from jsonschema import Draft4Validator
def jsonvalidation(json_doc_path, json_schema_path):
with open(json_schema_path, 'r') as fp:
schema_doc = json.load(fp)
# Open the file for reading
with open(json_doc_path, 'r') as fp:
json_doc = json.load(fp)
try:
validate(json_doc, schema_doc)
sys.stdout.write("Record passed validation \n")
except jsonschema.exceptions.ValidationError as incorrect:
v = Draft4Validator(schema_doc)
errors = sorted(v.iter_errors(json_doc), key=lambda e: e.path)
for error in errors:
sys.stderr.write("Record did not pass: \n")
sys.stderr.write(str(error.message) + "\n")
def main():
import argparse
parser = argparse.ArgumentParser(description='validate a JSON file')
parser.add_argument('jsondoc', type=str, help='path/to/doc.json')
parser.add_argument('schema', type=str, help='path/to/schema.json')
args = parser.parse_args()
jsonvalidation(args.jsondoc, args.schema)
if __name__ == '__main__':
main()