What Is JSON Schema
JSON Schema is a standard for describing and validating JSON data structures. It defines the properties, types, constraints, and defaults of JSON objects.
Example JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" },
"tags": { "type": "array", "items": { "type": "string" } }
},
"required": ["name", "email"]
}
Why You Need JSON Schema
- API documentation: Auto-generate API request/response structure docs
- Data validation: Validate JSON data correctness at runtime
- Form generation: Auto-generate form UI from Schema
- Mock data: Generate test data based on Schema
- Type safety: Generate TypeScript type definitions from Schema
- Config validation: Verify config file format is correct
JSON Schema Core Keywords
| Keyword | Description | Example |
|---------|-------------|---------|
| type | Data type | "string", "number", "boolean", "array", "object" |
| properties | Object properties | { "name": { "type": "string" } } |
| required | Required fields | ["name", "email"] |
| minimum/maximum | Number range | "minimum": 0, "maximum": 100 |
| minLength/maxLength | String length | "minLength": 1, "maxLength": 255 |
| enum | Enum values | ["active", "inactive", "pending"] |
| format | Format validation | "email", "uri", "date-time" |
| items | Array element type | { "type": "string" } |
How to Use an Online Tool
Using DevToolkit Pro's JSON Schema Generator:
- Paste a JSON object example
- The tool auto-analyzes the data structure
- Generates complete JSON Schema
- Supports custom constraints (required, ranges, etc.)
- One-click copy Schema code
Auto-Generate from JSON Example
Input
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"isActive": true,
"tags": ["developer", "admin"],
"address": { "city": "Shanghai", "zip": "200000" }
}
Output Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string" },
"isActive": { "type": "boolean" },
"tags": { "type": "array", "items": { "type": "string" } },
"address": {
"type": "object",
"properties": {
"city": { "type": "string" },
"zip": { "type": "string" }
}
}
}
}
JSON Schema Validation in Code
JavaScript (Ajv)
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
age: { type: 'integer', minimum: 0 }
},
required: ['name']
};
const validate = ajv.compile(schema);
const valid = validate({ name: 'Alice', age: 28 });
console.log(valid); // true
Python
from jsonschema import validate, ValidationError
schema = {
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name"]
}
try:
validate(instance={"name": "Alice", "age": 28}, schema=schema)
print("Validation passed")
except ValidationError as e:
print(f"Validation failed: {e.message}")
FAQ
What JSON Schema Versions Are Supported?
Draft 7 (2018-02) and 2020-12 are most commonly used. Draft 7 has the best compatibility — recommend using it first. Most validation libraries support Draft 7.
Can Schemas and TypeScript Interfaces Be Converted?
Yes. Use typescript-json-schema to generate Schema from TS, or json-schema-to-typescript to generate TS interfaces from Schema. Tools automate this process.
Can JSON Schema Validate Nested Objects?
Yes. JSON Schema natively supports nested object definitions using recursive properties. The $ref keyword references defined sub-schemas to avoid duplication.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
Complete Guide to YAML JSON Conversion: Principles, Tools, and Best Practices
Deep dive into the differences and connections between YAML and JSON. Master bidirectional YAML JSON conversion methods — online tools, CLI, code implementations, common pitfalls, and best practices.
Online XML/JSON Converter: Convert Between Data Formats
Learn how to convert between XML and JSON formats. Understand the differences between XML and JSON, and use cases in API integration, configuration management, and data exchange.
Online Markdown Preview: Real-time Render and Edit Markdown
Learn how to use an online Markdown previewer to render Markdown documents in real time. Understand Markdown syntax, GFM extensions, tables, code highlighting, and best practices for technical documentation.