Getting Started with JSON Schema
This guide walks you through creating your first JSON Schema and using it to validate JSON data. By the end, you’ll understand how to write schemas and apply them to real-world use cases.Prerequisites
You should be familiar with:- JSON syntax and data structures
- Basic programming concepts
No special tools are required to write a JSON Schema - it’s just JSON! However, you’ll need a JSON Schema validator to test your schemas.
Your First Schema
Let’s create a schema that validates a simple user profile. We’ll build it step by step.Step 1: Declare the Schema Version
Every schema should declare which JSON Schema version it uses with the$schema keyword:
Step 2: Define the Root Type
For a user profile object, we specify that the root must be an object:Step 3: Add Properties
Now let’s define the properties our user object can have:Step 4: Specify Required Properties
Let’s makeusername and email required:
Step 5: Add Metadata
Annotations liketitle and description make your schema self-documenting:
Complete Example
Here’s the complete schema with test instances:Validation Scenarios
Valid Instance
An instance with Result: VALID - All required fields present, all constraints satisfied.
username and email (both required), plus optional age and isActive fields matching their constraints.Missing Required Field
An instance missing the Result: INVALID - The
email field:required keyword specifies that email must be present.Using Reusable Schema Components
The$defs keyword lets you define reusable schemas within your document:
$defs and referenced with $ref.
The
$ref keyword uses JSON Pointer syntax. #/$defs/positiveInteger means “in this document, at the path /$defs/positiveInteger”.Example with User Roles
Advanced Validation: Schema Composition
JSON Schema provides keywords for combining schemas:allOf - Must Match All
anyOf - Must Match At Least One
"hello", 42, "hi"
oneOf - Must Match Exactly One
Validating Arrays
Arrays have special validation keywords:- Each item is a string starting with an uppercase letter
- There’s at least 1 item and at most 10 items
- All items are unique
["Apple", "Banana", "Cherry"]
Invalid: ["apple", "Banana"] (first item doesn’t start with uppercase)
Invalid: ["Apple", "Apple"] (items not unique)
Common Keywords Reference
Type Validation
| Keyword | Description | Example |
|---|---|---|
type | Specifies the instance type | {"type": "string"} |
enum | Instance must equal one of these values | {"enum": ["red", "green", "blue"]} |
const | Instance must equal this exact value | {"const": "active"} |
Numeric Constraints
| Keyword | Description | Example |
|---|---|---|
minimum | Minimum value (inclusive) | {"minimum": 0} |
maximum | Maximum value (inclusive) | {"maximum": 100} |
exclusiveMinimum | Minimum value (exclusive) | {"exclusiveMinimum": 0} |
exclusiveMaximum | Maximum value (exclusive) | {"exclusiveMaximum": 100} |
multipleOf | Must be a multiple of this value | {"multipleOf": 5} |
String Constraints
| Keyword | Description | Example |
|---|---|---|
minLength | Minimum string length | {"minLength": 3} |
maxLength | Maximum string length | {"maxLength": 100} |
pattern | Must match this regex | {"pattern": "^[A-Z]"} |
format | Semantic format (email, uri, etc.) | {"format": "email"} |
Array Constraints
| Keyword | Description | Example |
|---|---|---|
items | Schema for array items | {"items": {"type": "number"}} |
minItems | Minimum array length | {"minItems": 1} |
maxItems | Maximum array length | {"maxItems": 10} |
uniqueItems | Items must be unique | {"uniqueItems": true} |
Object Constraints
| Keyword | Description | Example |
|---|---|---|
properties | Define object properties | {"properties": {"name": {"type": "string"}}} |
required | Required property names | {"required": ["name", "email"]} |
minProperties | Minimum number of properties | {"minProperties": 1} |
maxProperties | Maximum number of properties | {"maxProperties": 5} |
Best Practices
Use
$id for reusable schemas - Assign a canonical IRI to schemas you plan to reference from other schemas.1. Start Simple
Begin with basic type and required field validation, then add constraints as needed:2. Add Descriptive Metadata
Usetitle and description to document your schema:
3. Use $defs for Reusable Components
Define common patterns once and reference them:
4. Validate Your Schemas
Test schemas against valid and invalid instances to ensure they work as expected. Use online validators or implement validation in your application.5. Consider Backward Compatibility
When evolving schemas:- Avoid making optional fields required
- Don’t remove fields that existing data uses
- Add new optional fields instead
Next Steps
You now have the foundation to create and use JSON Schemas! Here’s what to explore next:Schema Composition
Learn about
allOf, anyOf, oneOf, and not for complex validation logic.Schema References
Master
$ref, $id, and $anchor for modular, reusable schemas.Format Validation
Explore semantic validation with
format (email, uri, date-time, uuid, etc.).Conditional Schemas
Use
if, then, and else for conditional validation logic.