The Complete JSON Schema Guide (Draft 7)
JSON Schema is a contract for your data: it declares which properties must exist, what types they have, and what values are allowed. This guide covers the core Draft 7 keywords with a real-world API example.
What is JSON Schema?
A JSON Schema is itself a JSON document that describes the shape of other JSON data. Use it to validate API payloads, document expected formats, generate code (TypeScript, Go), and catch bad data before it reaches your logic.
Core keywords
The type keyword constrains a value to string, number, boolean, null, object, or array. properties and required describe object shape; items describes array elements.
{
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "number", "minimum": 0, "maximum": 150 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "email"]
}Beyond type: enum restricts to a fixed set, const requires an exact value, pattern matches a regex, minLength/maxLength bound string length, and additionalProperties: false forbids unknown keys.
A real-world API schema
Here's a schema for a user-registration endpoint. Notice how each constraint doubles as documentation, and how required + additionalProperties: false make the contract strict.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"username": { "type": "string", "pattern": "^[a-zA-Z0-9_]{3,30}$" },
"email": { "type": "string", "format": "email" },
"password": { "type": "string", "minLength": 8 },
"acceptTerms": { "type": "boolean", "const": true }
},
"required": ["username", "email", "password", "acceptTerms"],
"additionalProperties": false
}Best practices
Always declare $schema so validators know which draft you target. Add description fields — they become live API docs. Prefer format (email, uri) over hand-rolled regex. Use additionalProperties: false for strict APIs, but allow it when extensibility matters. Avoid deeply nested schemas — factor with $ref and definitions.
FAQ
- Which JSON Schema draft does this guide cover?
- Draft 7, the most widely supported version. It covers type, properties, required, items, enum, const, minimum/maximum, minLength/maxLength, pattern, format, additionalProperties, minItems/maxItems, and uniqueItems.
- Does TypeScript replace JSON Schema?
- No. TypeScript types are erased at compile time and provide no runtime validation. For API payloads, use JSON Schema (or Zod) to validate at runtime, and optionally generate TypeScript types from it.
- Can I auto-generate a schema from JSON?
- Yes — infer a schema from sample data, then refine it by adding constraints (required, pattern, length limits). Generation gives you a starting point, not a finished contract.
Related articles
What is JSON? A Complete Beginner's Guide →
JSON (JavaScript Object Notation) is a lightweight data-interchange format that's easy for humans to read and for machines to parse. Learn the syntax, data types, and structure that power modern APIs.
The History of JSON: From 2000 to Industry Standard →
Trace JSON from Douglas Crockford's idea in 2001, through Yahoo's adoption, to becoming the ECMA-404 standard that powers 90% of modern APIs.
JSON Examples in 7 Languages: JavaScript, Python, Go, Rust, PHP, Java, C# →
Production-ready JSON parsing and serialization examples across seven languages — with error handling and best practices.