JSON Schema Generator
Generate a JSON Schema draft from sample JSON for validation, documentation, and API design.
Back to all tools on ToolForge
Generated Schema
About JSON Schema Generator
This JSON Schema generator creates a schema draft by analyzing sample JSON data. It infers types (string, number, boolean, array, object, null), detects object properties, and marks all fields as required by default.
The generated schema uses JSON Schema Draft 2020-12 format and serves as a starting point for API contracts, validation rules, and documentation. You may need to refine the output to add constraints like patterns, ranges, or optional fields.
JSON Schema Type Reference
| Type | Schema Example | Sample Value |
|---|---|---|
string | {"type": "string"} | "hello" |
number | {"type": "number"} | 42.5 |
integer | {"type": "integer"} | 42 |
boolean | {"type": "boolean"} | true |
array | {"type": "array", "items": {...}} | [1, 2, 3] |
object | {"type": "object", "properties": {...}} | {"key": "value"} |
null | {"type": "null"} | null |
Common String Formats
| Format | Description | Example |
|---|---|---|
email | Email address | [email protected] |
uri | Full URL | https://example.com |
date | ISO 8601 date | 2026-03-25 |
date-time | ISO 8601 datetime | 2026-03-25T10:30:00Z |
time | ISO 8601 time | 10:30:00Z |
uuid | UUID format | 550e8400-e29b-41d4-a716-446655440000 |
ipv4 | IPv4 address | 192.168.1.1 |
ipv6 | IPv6 address | 2001:0db8:85a3::8a2e:0370:7334 |
Schema Constraints Reference
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100,
"pattern": "^[a-zA-Z ]+$"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"email": {
"type": "string",
"format": "email"
},
"website": {
"type": "string",
"format": "uri"
},
"tags": {
"type": "array",
"items": {"type": "string"},
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
},
"metadata": {
"type": "object",
"minProperties": 1,
"additionalProperties": true
}
},
"required": ["name", "email"]
}
Inference Algorithm
How this generator works:
1. Parse JSON input
2. For each value:
- null → {"type": "null"}
- boolean → {"type": "boolean"}
- number → {"type": "number"} (or "integer" if whole)
- string → {"type": "string"}
- array → {"type": "array", "items": schemaOf(firstElement)}
- object → {"type": "object", "properties": {...}, "required": [...]}
3. Wrap in draft-2020-12 schema with $schema declaration
Limitations:
- Arrays: Only analyzes first item for items schema
- Required: Marks ALL object fields as required
- Unions: Doesn't detect multiple possible types
- Enums: Doesn't infer allowed value sets
- Formats: Doesn't detect email, uri, date formats
- Constraints: Doesn't infer min/max lengths or values
Refine the generated schema to add these details.
Validation Example with Ajv
// Install: npm install ajv
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name"]
};
const validate = ajv.compile(schema);
const data1 = {name: "John", age: 30};
const data2 = {age: 30}; // missing required name
const data3 = {name: "John", age: -5}; // invalid minimum
console.log(validate(data1)); // true
console.log(validate.errors); // null
console.log(validate(data2)); // false
console.log(validate.errors); // [{keyword: "required", ...}]
console.log(validate(data3)); // false
console.log(validate.errors); // [{keyword: "minimum", ...}]
Frequently Asked Questions
- What is JSON Schema?
- JSON Schema is a vocabulary for validating JSON documents. It defines the structure, data types, and constraints for JSON data. JSON Schema is used for API contracts, form validation, documentation, and ensuring data consistency. The schema itself is written in JSON format.
- What are the basic JSON Schema types?
- JSON Schema supports these core types: string, number, integer, boolean, array, object, and null. The type keyword can be a single string or array of allowed types. Example: {"type": "string"} or {"type": ["string", "null"]} for nullable strings.
- How does JSON Schema inference work?
- Schema inference analyzes sample JSON to guess the structure: primitives become their type, arrays become {type: 'array', items: {...}}, objects become {type: 'object', properties: {...}, required: [...]}. Limitations: infers from first array item only, doesn't detect optional fields, may miss union types.
- What is the $schema keyword?
- $schema declares which JSON Schema draft/version the schema uses. Example: "$schema": "https://json-schema.org/draft/2020-12/schema". This helps validators apply correct rules. Common versions: draft-07, draft/2019-09, draft/2020-12. Always include $schema for compatibility.
- How do I validate JSON against a schema?
- Use a JSON Schema validator library: Ajv (JavaScript), jsonschema (Python), json-schema-validator (Java), or online tools. Validation checks if data matches the schema's type, required fields, formats, and constraints. Returns validation errors for mismatched data.
- What are common JSON Schema constraints?
- String: minLength, maxLength, pattern (regex), format (email, uri, date). Number: minimum, maximum, exclusiveMinimum, exclusiveMaximum. Array: minItems, maxItems, uniqueItems. Object: required, minProperties, maxProperties, additionalProperties. These constraints refine type definitions.