JSON Schema is a way to describe what a piece of JSON should look like: which fields it must have, what type each one is, and what values are allowed. Think of it as a contract. You write the rules once, in JSON, and any program in any language can check data against them. That’s how APIs reject bad requests, editors autocomplete config files, and forms validate input before it’s sent.
This beginner’s guide explains the core ideas with examples you can copy, shows where JSON Schema is used, and how to generate one from sample data instead of writing it from scratch.
A simple example
Say a sign-up API accepts this:
{
"email": "[email protected]",
"name": "Rahim Uddin",
"age": 29,
"plan": "pro",
"newsletter": true
}Here is a schema that describes it:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Sign-up",
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" },
"name": { "type": "string", "minLength": 1, "maxLength": 80 },
"age": { "type": "integer", "minimum": 13 },
"plan": { "enum": ["free", "basic", "pro", "scale"] },
"newsletter": { "type": "boolean", "default": false }
},
"required": ["email", "name", "plan"],
"additionalProperties": false
}Read it top to bottom and it’s almost plain English: the data is an object; email is a string in email format; age is a whole number of at least 13; plan must be one of four values; email, name and plan are required; and no other fields are allowed.
Now {"email": "[email protected]", "plan": "gold"} fails for two reasons: name is missing, and "gold" isn’t an allowed plan. A validator reports both, with the path to each problem.
The key building blocks
| Keyword | What it does | Example |
|---|---|---|
type | The kind of value | "string", "number", "integer", "boolean", "object", "array", "null" |
properties | Rules for each field of an object | {"email": {"type": "string"}} |
required | Fields that must be present | ["email", "name"] |
additionalProperties | Whether unknown fields are allowed | false to reject typos like emial |
items | Rules for every element of an array | {"type": "string"} |
enum | A fixed list of allowed values | ["free", "pro"] |
minimum / maximum | Number limits | "minimum": 0 |
minLength / maxLength / pattern | String length and regex rules | "pattern": "^01[3-9][0-9]{8}$" for a BD mobile number |
format | Common string formats | "email", "date", "date-time", "uri" |
$ref / $defs | Define a shape once and reuse it | {"$ref": "#/$defs/address"} |
oneOf / anyOf | Allow one of several shapes | A value that’s a number or null |
Arrays and nested objects
Schemas nest the same way data does. An order with a list of line items:
{
"type": "object",
"properties": {
"order_id": { "type": "string" },
"items": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"sku": { "type": "string", "pattern": "^[0-9]{4}$" },
"qty": { "type": "integer", "minimum": 1 }
},
"required": ["sku", "qty"]
}
}
},
"required": ["order_id", "items"]
}Reusing parts with $ref
When the same shape appears in several places, such as an address used for billing and shipping, define it once under $defs and point to it:
{
"$defs": {
"address": {
"type": "object",
"properties": { "city": { "type": "string" }, "zip": { "type": "string" } },
"required": ["city"]
}
},
"type": "object",
"properties": {
"billing": { "$ref": "#/$defs/address" },
"shipping": { "$ref": "#/$defs/address" }
}
}Where JSON Schema is used
- API validation. Backends check incoming requests against a schema and return a clear error instead of crashing or storing bad data.
- API documentation. OpenAPI (Swagger) describes request and response bodies with JSON Schema.
- Editor autocomplete. VS Code uses schemas to suggest and check fields in files like
package.jsonandtsconfig.json. - Forms. Form libraries can build and validate a form straight from a schema.
- Data pipelines. Checking imports and exports before they reach a database.
- AI tools. Many AI APIs use JSON Schema to define structured outputs and tool inputs.
Generate a schema from sample data
Writing a schema from scratch is slow. A faster route is to generate a first draft from real data and then tighten it:
- Open Doetra and paste a sample of your JSON, or drop the file.
- Choose JSON Schema as the output.
- Copy the result into your project.
A generated schema describes what the sample looked like, so refine it with what you know about the data:
- Add
requiredfor fields that must always be present. - Replace plain strings with
enumwhere only a few values are allowed. - Add
format,minimum,maxLengthandpatternrules. - Allow
nullwhere a field can be empty:"type": ["string", "null"]. - Decide on
additionalProperties:falsefor strict APIs, left open for data that may grow.
Doetra runs in your browser, so samples with real customer data aren’t uploaded. It can also generate TypeScript types or a Zod schema from the same JSON.
JSON Schema vs TypeScript types
TypeScript types check your code while you write it, then disappear when it’s compiled. JSON Schema checks data while the program runs, and works in any language. If data comes from outside (users, APIs, files), you need a runtime check like JSON Schema or Zod. Types alone won’t catch a bad request.
Common mistakes
- Forgetting
required. Without it, every property is optional, so an empty object{}passes. - Misspelled keywords. Unknown keywords like
"requried"are silently ignored, so the rule never runs. Test the schema with data that should fail. - Expecting
formatto be enforced. Some validators treatformatas a note only unless you switch format checking on. - Mixing drafts. Keywords changed between versions (
definitionsbecame$defs). Set$schemaand use one draft consistently.
Frequently asked questions
Is JSON Schema a programming language?
No. It’s a vocabulary written in plain JSON that describes data. Validators in JavaScript, Python, PHP, Go, Java and many other languages read it.
Which draft should I use?
Use 2020-12 for new projects unless a tool you depend on requires an older draft. OpenAPI 3.1 is aligned with 2020-12.
Can I create a JSON Schema for free?
Yes. Paste sample JSON into Doetra and choose JSON Schema as the output. No sign-up, nothing uploaded.
How do I validate JSON against a schema?
Use a validator library in your language, such as Ajv for JavaScript or jsonschema for Python, or an online validator for quick checks.