JSON Schema Explained: Validation, Rules and Examples
JSON Schema defines rules for JSON data. It allows developers to validate JSON structures, require specific fields and ensure data matches expected formats.
- JSON Schema defines rules for JSON data.
- It validates structure, fields and data types.
- JSON Schema improves API reliability and consistency.
- It is commonly used in APIs, applications and integrations.
What is JSON Schema?
JSON Schema is a specification used to describe and validate JSON data structures.
It allows developers to define:
- Required fields
- Allowed data types
- Minimum and maximum values
- String formats
- Nested object structures
JSON is the data. JSON Schema is the rulebook explaining what the data should look like.
Simple JSON example
{
"name": "Jane Smith",
"role": "Chief Cheese Tester",
"active": true
}
Simple JSON Schema example
This schema defines the expected structure for the JSON above.
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"role": {
"type": "string"
},
"active": {
"type": "boolean"
}
},
"required": [
"name",
"role"
]
}
How JSON Schema validation works
- A JSON document is provided.
- A validator checks the document against the schema.
- The validator confirms whether the JSON follows the rules.
- Errors are returned if validation fails.
Common JSON Schema rules
| Rule | Purpose |
|---|---|
type |
Defines allowed data type. |
required |
Specifies mandatory fields. |
minLength |
Sets minimum string length. |
maximum |
Defines maximum numeric value. |
enum |
Restricts values to a predefined list. |
Required fields example
The schema below requires both name and role.
{
"required": [
"name",
"role"
]
}
If either field is missing, validation fails.
Invalid JSON example
This JSON would fail validation if the schema requires a role field.
{
"name": "Jane Smith"
}
The required role property is missing. JSON Schema validators would reject this data structure.
JSON Schema vs basic JSON validation
| Validation type | Checks |
|---|---|
| Basic JSON validation | Checks JSON syntax only. |
| JSON Schema validation | Checks structure, required fields and rules. |
Where JSON Schema is used
- REST APIs
- OpenAPI specifications
- Configuration validation
- Frontend form validation
- Backend data validation
- Third-party integrations
JSON Schema best practices
- Keep schemas simple and readable.
- Use required fields carefully.
- Validate data early in workflows.
- Document schemas clearly.
- Reuse schema components where possible.
Validate and format JSON online
Use CheeseBridge JSON tools to validate, format and inspect JSON structures before applying schema validation.
Open JSON Formatter Open JSON ViewerTrusted JSON Schema references
Frequently asked questions
What is JSON Schema used for?
JSON Schema is used to validate and define rules for JSON data structures.
Does JSON Schema validate syntax?
JSON Schema validates structure and rules, but the JSON must already be syntactically valid first.
Can JSON Schema require fields?
Yes. JSON Schema can specify required properties using the required keyword.