Skip to content
Tutorials10 min read

The complete JSON validation guide

How to validate JSON at every layer — parser, JSON Schema, and business logic — with practical examples.

By JSON Formatter Team

Three layers of validation

Robust systems validate JSON at three distinct layers. Each catches a different class of bug.

Layer 1 — Syntax

Is the input parseable at all? Use the runtime's built-in parser (JSON.parse, json.loads, serde_json::from_str). Syntax errors should be surfaced with line and column to the developer — our JSON Validator does this instantly.

Layer 2 — Structure (JSON Schema)

Is the shape correct? Required fields present, types matching, enums respected? JSON Schema is the standard. Frameworks: Ajv (Node), jsonschema (Python), Newtonsoft.Json.Schema (.NET).

Layer 3 — Business rules

Even a syntactically and structurally valid payload can be semantically wrong: end date before start date, negative price, invalid country code. This is application-level validation and lives close to the domain.

Where to run each layer

  • Layer 1: at the API boundary, always.
  • Layer 2: at the API boundary and in tests.
  • Layer 3: inside the service, close to the transaction.

Fail loudly, fail early

Return 422 Unprocessable Entity with a machine-readable error body pointing at the offending JSON path. Consumers thank you.