A validator has been provided to verify that the JSON being sent to and from devices is validated against the provided JSON Schema and meets the interface requirements in the schema.
from jsonschema import validate
from jsonschema.exceptions import ValidationError
import os, simplejson as json
# Uses 2 data files json and schema
def schema_validation(json_file, schema_file):
site_root = os.path.realpath(os.path.dirname(__file__)) # Define open file
# Open json file
file_j = os.path.join(site_root, 'static', json_file)
with open(file_j, 'r') as j:
json_data = j.read()
json_obj = json.loads(json_data)
j.close()
# Open schema file
file_s = os.path.join(site_root, 'static', schema_file)
with open(file_s, 'r') as s:
schema_data = s.read()
schema = json.loads(schema_data)
s.close()
try:
result = validate(json_obj, schema)
print(result)
except ValidationError as config_error:
print("Config JSON is malformed! {}".format(config_error))
Enter JSON Object file and Schema file into the Validator.
INPUT:
json_object_file = 'SAM_JSON_TEST_DB.json'
schema_build_file = 'GenSchema_db.json'
OUTPUT:
Valid: True or None
Once the JSON Object file has been validated, you can begin to use it.
Invalid: Shows an Error