JSON Validation
Explore how to use the jsonschema package in Python to validate JSON documents against defined schemas. Learn to check data integrity at runtime, handle newline-delimited JSON samples, and incorporate schemas into Python classes for safer and more reliable JSON processing.
Overview
We noted that our mypy type hint doesn’t really guarantee the JSON document is—in any way—what we expected. There is a package in the Python Package Index that can be used for this. The jsonschema package lets us provide a specification for a JSON document, and then confirm whether or not the document meets the specification.
The JSON Schema validation is a runtime check, unlike the mypy type hint. This means using validation will make our program slower. It can also help to diagnose subtly incorrect JSON documents. For details, visit https://json-schema.org. This is evolving toward standardization, and there are several versions of compliance checking available.
We’ll focus on newline-delimited ...