Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Python - Validate JSON

The snippet below shows you how you can validate a JSON file against a JSON schema.

Related Posts

Steps

import json
import jsonschema

# A sample schema, like what we'd get from json.load()
with open('schema.json') as schemaFile:
    schema = json.load(schemaFile)
    jsonschema.Draft7Validator.check_schema(schema)
    print("Schema is valid")

with open('data.json') as dataFile:
    data = json.load(dataFile)

validator = jsonschema.Draft7Validator(schema).validate(data)

# Wont get to here if validation fails, as will throw exception
print("Validation passed.")
Last updated: 16th June 2021
First published: 16th June 2021