Lesson 8
JSON Schema Basics
Describe and validate document shape with types, required fields, and constraints.
JSON Schema is a vocabulary for annotating JSON documents with rules: which properties exist, their types, and value constraints. Teams use it to validate API payloads, config files, and generated data.
A minimal schema
This schema describes an object with a required string name and optional number age:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"],
"additionalProperties": false
}
Valid instance:
{ "name": "Ada", "age": 30 }
Invalid: missing name, wrong type, or extra unknown keys when additionalProperties is false.
Core keywords
| Keyword | Purpose |
|---|---|
type | string, number, integer, boolean, array, object, null |
properties | Shape of object fields |
required | Mandatory property names |
items | Schema for each array element |
enum | Allowed literal values |
minLength / maxLength | String bounds |
Why schemas matter
- Contracts between frontend and backend teams
- Automated validation in CI before deploy
- Documentation that stays machine-readable
- OpenAPI often embeds or references JSON Schema for request bodies
Validation workflow
- Author or generate a schema for your expected payload.
- Run a validator (library or tool) against incoming JSON.
- Fix data or schema when real-world edge cases appear—schemas evolve like code.
Schemas do not replace understanding JSON syntax from earlier lessons—they sit on top, describing allowed structure once JSON is already parseable.