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

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch