What is JSON Schema?
JSON Schema is a declarative standard for annotating and validating JSON data structures. It provides a formal contract defining the expected shape, primitive types, required fields, and range boundaries of a JSON payload.
By validating incoming network payloads against a predefined JSON Schema, modern REST APIs can reject malformed requests at the API Gateway layer before they reach business logic.
Direct Comparison & Benefits
- Automated Request Validation: Eliminates manual checks.
- Interactive API Documentation: Generate OpenAPI (Swagger) documentation automatically.
- Contract Testing: Ensure backend services and frontend clients strictly adhere to agreed data contracts.
Anatomy of a Basic JSON Schema
Here is a schema validating a user payload:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "UserRegistration",
"type": "object",
"properties": {
"userId": { "type": "integer", "minimum": 1 },
"username": { "type": "string", "minLength": 3 },
"email": { "type": "string", "format": "email" }
},
"required": ["userId", "username", "email"]
}
Validate Your JSON Online
Format, validate, and minify your JSON schemas and payloads instantly in your browser at JSON Workshop.