A json schema validator in Python answers a different question from a syntax check. Python first parses the text into data, then the jsonschema package tests that data against rules such as required fields, types and allowed values. The input is a JSON file plus a schema file, and the output is a list of violations with a location and the failed keyword. Before writing any script, I confirm the text is well-formed with the HTML Editor Online JSON Validator, which checks syntax only. Once the text parses, the Python workflow below tells you exactly what is wrong with the structure and what to fix first.
Table of Contents

Syntax errors and schema violations are different problems
JSON has two layers of correctness. The first is syntax. RFC 8259 defines what counts as JSON text: quoted keys, matched brackets, no trailing commas. A parser either accepts the text or fails and reports a position.
The second layer is structure, described by JSON Schema. A schema states which properties must exist, what type each value has and which values are allowed. A document can pass the first layer and still fail the second.
| Check | Question it answers | Typical failure |
|---|---|---|
| Syntax | Can this text be parsed as JSON? | A missing closing brace or a trailing comma |
| Schema | Does the parsed data follow the rules? | A required field is missing, or a number arrives as a string |
The order matters because a schema validator needs parsed data. A syntax error stops everything before any schema rule is tested. So fix syntax first, then make sure the schema itself is valid, then fix the data.

Check the JSON before you run a JSON schema validator in Python
Paste the text into the JSON Validator first. It checks syntax with the browser’s JSON.parse and gives you a normalized copy, with optional key sorting. The JSON Editor offers the same JSON.parse syntax validation along with highlighting, code folding and format or minify, which helps when you want to edit as you fix. Neither tool does JSON Schema validation, and the editor has no tree view. A passing result means only that the text parses.
If parsing fails, read the reported error and fix it at the source. For common formatting mistakes, JSON Repair can attempt an automatic fix. Review its output carefully, because a repair can change values, and it cannot recover data that was truncated. Treat it as a way to get a file moving, then correct whatever produced the broken JSON.
When the JSON parses but sits on one dense line, the JSON Formatter pretty-prints it with indentation so field names and nesting are easy to read. Formatting changes presentation only. It does not change which schema rules the data satisfies, so a formatted file can still fail validation.

A small schema and two data files to test with
Save this as schema.json. It uses Draft 2020-12 and describes an object with three fields.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"status": { "type": "string", "enum": ["active", "inactive"] }
},
"required": ["name", "age", "status"]
}Save this valid example as data.json.
{"name": "Ada", "age": 32, "status": "active"}Then save a second file, bad.json. It is perfectly good JSON syntax, but it breaks three rules.
{"age": "32", "status": "pending"}Here name is missing, age is a string instead of an integer, and status is outside the enum. Two details are easy to miss. Listing a field under properties does not make it mandatory; only required does. And extra properties are allowed by default unless the schema restricts them, for example with additionalProperties set to false, as described in the object keyword reference.
A JSON schema validator in Python: the full script
Install the package with pip. The stable documentation I checked on November 21, 2026 lists version 4.26.0 and this install command, so see the jsonschema documentation for current details.
pip install jsonschema
Save the script as validate.py in the same folder as your JSON files.
import json
import sys
from jsonschema import Draft202012Validator
from jsonschema.exceptions import SchemaError
data_file = sys.argv[1] if len(sys.argv) > 1 else 'data.json'
try:
with open('schema.json', encoding='utf-8') as file:
schema = json.load(file)
with open(data_file, encoding='utf-8') as file:
data = json.load(file)
except json.JSONDecodeError as error:
raise SystemExit(f'JSON syntax error at line {error.lineno}, column {error.colno}: {error.msg}')
except OSError as error:
raise SystemExit(f'Cannot read file: {error}')
try:
Draft202012Validator.check_schema(schema)
except SchemaError as error:
raise SystemExit(f'Invalid schema: {error.message}')
validator = Draft202012Validator(schema)
errors = sorted(
validator.iter_errors(data),
key=lambda error: (tuple(map(str, error.path)), error.message),
)
if errors:
for error in errors:
location = '.'.join(map(str, error.path)) or '(root)'
print(f'{location} [{error.validator}]: {error.message}')
raise SystemExit(1)
print('Data matches the schema')The script works in three stages, and each stage fails with its own message. json.load() parses a file and raises json.JSONDecodeError on a decoding failure, with lineno, colno and msg attributes. That is your syntax stage. check_schema() confirms the schema is itself a valid schema, so a typo in the schema is reported as a schema problem instead of a confusing data error. iter_errors() then yields every violation rather than stopping at the first, which lets you fix several problems per run.
Read the errors and decide what to fix first
Run the script on both files.
python validate.py data.json python validate.py bad.json
The valid file prints the success line. The bad file exits with status 1 and prints one line per violation. The output looks roughly like this, though exact wording and ordering can differ between library versions, so do not build logic around the message text.
(root) [required]: 'name' is a required property age [type]: '32' is not of type 'integer' status [enum]: 'pending' is not one of ['active', 'inactive']
Each line has three parts. error.path is the location inside the data, error.validator names the failed keyword and error.message describes the problem. A missing required property is reported against the containing object, so its path is empty, which the script prints as (root).
| Keyword | What it means | Usual fix |
|---|---|---|
required | A mandatory property is absent | Add the field in the data, or the producer’s output |
type | A value has the wrong JSON type | Send a number, not a quoted string |
enum | The value is not in the allowed list | Use an allowed value, or update the schema deliberately |
Work in this order:
- Resolve any JSON syntax error reported by
json.load()or by the syntax tools. - Fix an
Invalid schemamessage, because data results mean nothing against a broken schema. - Address missing required fields, then wrong types, then disallowed enum values.
- Rerun the script until it prints the success line.
Limits to keep in mind before you trust a pass
A passing run proves less than it seems. Python’s standard decoder accepts some nonstandard values, including NaN, by default, so a successful json.load() does not guarantee strict JSON syntax. That is why the earlier strict browser-based check is still worth doing. The json module also documents a parse_constant hook if you want to reject those values in code.
Remember what each tool covers. The online syntax tools do not perform schema validation, and the Python script does not tell you whether values are semantically right. An age of 3 passes an integer check even if the real person is 30. Add rules such as minimum or additionalProperties when you need them.
Large numbers deserve a special note. The browser’s JSON.parse can round long integers, so a normalized copy from a browser tool may differ from your source. Read the write-up on large integer IDs changing after validation, and keep long identifiers as strings in your schema and data.
For more habits that make files easier to review, see these tips for formatting JSON, and the guide to editing JSON in the browser if you work with larger files.
Frequently asked questions
How do I validate JSON against a schema in Python?
Install the jsonschema package, load the schema and the data with json.load(), call Draft202012Validator.check_schema() on the schema, then loop over validator.iter_errors(data). If the list is empty, the data matches the schema. The script in this article does exactly that.
What is the difference between JSON syntax validation and JSON Schema validation?
Syntax validation asks whether text can be parsed as JSON at all. Schema validation asks whether the parsed data follows rules such as required properties, types and allowed values. A file can pass the first and fail the second, and the first has to succeed before the second can run.
Does the online JSON Validator check against a JSON Schema?
No. The JSON Validator checks syntax with the browser’s JSON.parse and returns a normalized copy. The JSON Editor also validates syntax only and has no JSON Schema validation. Use the Python script for structure rules.
Does JSON Schema validate Python data or only JSON text?
The jsonschema library validates the parsed Python object, such as a dict or list, not the raw text. That is why json.load() runs first. If parsing fails, you get a JSONDecodeError and no schema checks happen.
Why does my data pass when a field is listed under properties?
Listing a field under properties only describes what the field should look like if it is present. It does not make the field mandatory. Add the field name to the required array to make a missing field an error.
Are extra properties allowed by default?
Yes. Unless the schema restricts them, for example with additionalProperties set to false, an object can contain fields the schema never mentions. This means a misspelled field name can slip through, so add that restriction when you want strict objects.
Can JSON Repair or the JSON Formatter fix schema violations?
No. JSON Repair addresses common formatting mistakes and can change values, so review its output. The JSON Formatter only indents parseable data. A missing field, wrong type or disallowed enum value has to be corrected in the data or its producer, then validated again.
Next steps
A reliable json schema validator in Python workflow has a clear order: confirm the text is well-formed, make sure the schema is valid, then read each violation by location and keyword. Save the sample files, run the script on both, then break one rule at a time in data.json and watch which message changes. When you adapt it, replace the sample schema with your own and keep a strict syntax check as the first step.
Related tools and resources on HTML Editor Online:
