TechCompare LogoTechCompare

What is JSON Schema? Validate API payloads, config files, and data integrity

Use JSON Schema for public APIs where you can't control the client, for critical data pipelines where bad data is expensive, and for configuration files where a typo could break production. For internal microservices with shared type definitions (TypeScript, Protobuf), the schema adds overhead that may not be worth it.

JSON Schema is a vocabulary that lets you annotate and validate JSON documents. You define the expected shape of your data (required fields, types, value ranges, regex patterns) in a schema file, and any JSON document can be validated against it. It's the JSON equivalent of XML Schema or a database table definition.

By TechCompare · Updated

Topic
JSON Schema validation
json-schema
Category
Best Practices
JSON knowledge base

How this is calculated

JSON Schema is used extensively in OpenAPI/Swagger specs for REST APIs, in VS Code's settings validation, and in data pipeline validation. It turns runtime type errors into build-time or deploy-time validation failures. A well-written schema also doubles as documentation: any developer reading the schema knows exactly what fields an API endpoint expects and what format they should be in. The trade-off is that writing and maintaining schemas takes effort, and schema drift (where the schema and the actual code diverge) is a real maintenance hazard.

Verdict

The mechanism is what makes the value case clear. A schema file specifies the expected shape, types, value ranges, and regex patterns of a JSON document, and any inbound payload can be validated against it at build or deploy time, which turns what would be a runtime type error into a build-time failure that gets caught before it ships. That's the payoff for public APIs (where you can't control the client's payload) and for critical data pipelines (where bad data is expensive). The schema also doubles as documentation, since any developer reading it knows exactly what fields an endpoint expects. The maintenance hazard is schema drift, where the schema and the actual code diverge, which is the overhead that pushes internal microservices with shared TS or Protobuf definitions toward skipping the schema and relying on those shared types directly.

More JSON scenarios

Frequently asked questions

Is JSON Schema worth the effort?
For public APIs and critical data validation, yes. The upfront cost of writing schemas pays for itself in reduced debugging time and clearer documentation. For internal tools with a small team, the overhead may not be justified.
What's the minimum schema syntax to validate an email field?
`{ "type": "string", "format": "email" }`. The `format` keyword is a soft validator that suggests the value should be an email format. The hard validator is `pattern`, which takes a regex string like `"^\\S+@\\S+\\.\\S+$"`. For strict validation use both: format checks structure, pattern enforces it.
How do I validate a nested array of objects in JSON Schema?
Use the `items` keyword with a child schema. For example: `{ "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" } }, "required": ["id", "name"] } }`. This validates every item in the array against the object schema. Add `minItems` or `uniqueItems` for further constraints.