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 in OpenAPI specifications, editor support, and data validation. The schema describes expected data, but it doesn't validate payloads by itself. A validator can check sample data during a build or check each incoming request at runtime. A well-written schema also documents the fields an endpoint expects. The trade-off is the effort of maintaining the schema and keeping it aligned with the code.

Verdict

A JSON Schema declares the expected fields, types, ranges, and patterns. A validator must execute it: run sample checks during development or CI, and validate each incoming payload at runtime when the application needs to reject bad requests. A schema alone doesn't turn a runtime error into a build error. Schemas also document endpoint requirements, though they need to stay aligned with the code. For internal services with shared TypeScript or Protobuf definitions, that maintenance work may not be worthwhile.

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" }` is the usual starting point. In the 2019-09 and 2020-12 drafts, `format` is an annotation unless the validator enables the format-assertion vocabulary, so implementations can differ. A `pattern` such as `^\\S+@\\S+\\.\\S+$` is an assertion, but it is only a coarse check and isn't a strict email grammar. Use the schema for a baseline, then apply the application's email policy or a dedicated validator when strict validation matters.
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.