JSON Schema: Validate Your Data Structure
JSON Schema is a vocabulary that lets you annotate and validate JSON documents. It defines the expected structure, data types, and constraints for your JSON data. Think of it as TypeScript types, but for any JSON data — not just JavaScript objects.
What is JSON Schema?
JSON Schema is itself written in JSON. It describes what valid data looks like: which properties are required, what types they should be, value ranges, string patterns, and more. Validators use the schema to check whether a JSON document conforms to the expected structure.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1},
"age": {"type": "integer", "minimum": 0},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "email"]
}Core Keywords
JSON Schema uses keywords to define constraints. Here are the most commonly used ones:
- type — Specifies the data type:
"string","number","integer","boolean","array","object","null" - required — Array of property names that must be present
- properties — Defines the schema for each property of an object
- items — Defines the schema for array elements
- enum — Restricts a value to a fixed set of options
- pattern — Validates a string against a regular expression
Practical Example: User Registration
Here's a realistic JSON Schema for validating user registration data. It ensures the username is between 3-20 characters, the email is valid, the password meets minimum length requirements, and the age is within a reasonable range.
{
"type": "object",
"properties": {
"username": {
"type": "string",
"minLength": 3,
"maxLength": 20,
"pattern": "^[a-zA-Z0-9_]+$"
},
"email": {
"type": "string",
"format": "email"
},
"password": {
"type": "string",
"minLength": 8
},
"age": {
"type": "integer",
"minimum": 13,
"maximum": 120
}
},
"required": ["username", "email", "password"]
}Where to Use JSON Schema
JSON Schema is used across the development stack for data validation:
- API Validation — Validate request bodies before processing (Express, FastAPI, Spring Boot)
- Form Validation — Generate forms from schemas and validate input client-side
- Documentation — OpenAPI (Swagger) uses JSON Schema to describe API data models
- Configuration — VS Code uses JSON Schema to provide autocomplete for settings.json
- Testing — Validate API responses against expected schemas in integration tests
JSON Schema vs TypeScript
If you work with TypeScript, you might wonder why you need JSON Schema when you already have type checking. The key difference is that TypeScript types only exist at compile time — they're erased when your code runs. JSON Schema validates data at runtime, which is essential for external data (API inputs, file uploads, user input).
You can use PureJSON's JSON to TypeScript converter to generate TypeScript interfaces from your data, and JSON Schema for runtime validation — they complement each other.
Try These Tools
Frequently Asked Questions
Is JSON Schema hard to learn?
The basics are straightforward — you can start validating data with just type, required, and properties. Advanced features like conditional schemas and references take more time but aren't needed for most use cases.
What tools validate JSON against a schema?
Popular validators include Ajv (JavaScript), jsonschema (Python), and everit-org/json-schema (Java). Most web frameworks have built-in or plugin-based JSON Schema validation support.