Developer Documentation
Python
Updated September 19, 2026

Validate JSON in Python

Validate JSON syntax and handle parser errors without mutating the input in Python.

Syntax validation answers one question: can the input be parsed as JSON? It is different from JSON Schema validation, which checks the shape and constraints of already valid JSON data.

Quick Start#

The following example uses the standard Python API or a widely used package for this task.

Syntax validation#

Python
1import json23def validate_json(source: str) -> tuple[bool, str | None]:4    try:5        json.loads(source)6        return True, None7    except json.JSONDecodeError as exc:8        return False, f"{exc.msg} at line {exc.lineno}, column {exc.colno}"910print(validate_json('{"ok": true}'))

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

JSON Schema#

Python
1# pip install jsonschema2from jsonschema import validate34schema = {5    "type": "object",6    "required": ["name"],7    "properties": {"name": {"type": "string"}},8}910validate(instance=json.loads(source), schema=schema)

Important Notes#

json.loads validates syntax only. Use a JSON Schema library or application-level checks when structure and field constraints matter.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement JSON Validator in other languages: