import json
from jsonschema import validate, Draft7Validator, ValidationError
def load_json_file(filepath):
with open(filepath, "r") as f:
return json.load(f)
def validate_json(json_data, schema):
validator = Draft7Validator(schema)
errors = sorted(validator.iter_errors(json_data), key=lambda e: e.path)
return errors
def main(schema_file, data_file):
schema = load_json_file(schema_file)
json_data = load_json_file(data_file)
errors = validate_json(json_data, schema)
if not errors:
print("✅ JSON is valid against the schema.")
else:
print(f"❌ Found {len(errors)} validation error(s):")
for error in errors:
path = ".".join([str(p) for p in error.path])
print(f"• {path or '[root]'}: {error.message}")
if __name__ == "__main__":
# Replace with your filenames or make it command-line-driven
schema_path = "output_schema.json"
json_path = "sample_data.json"
main(schema_path, json_path)
No comments:
Post a Comment