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


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)
    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: 5th March 2022
First published: 16th June 2021