JSON Schema Validator and Builder with Auto-Generation
JSON Schema is the contract language of modern APIs. It defines what data your API accepts and returns, catches malformed requests before they reach your application logic, and generates documentation and client SDKs automatically. But writing JSON Schema by hand is tedious and error-prone: you need to remember the correct keywords for each constraint, handle nested objects and arrays, and keep the schema synchronized with your actual data structures. This tool automates the process by inferring schemas from example JSON data, validating data against schemas in real time, and providing an interactive builder for refining the generated output.
Paste example JSON in the left panel and the tool generates a complete draft-07 schema with inferred types, required properties, string format detection (emails, URLs, dates, UUIDs), and nested object schemas. Then paste any JSON in the validator to check it against the schema instantly, with detailed error messages showing exactly which property failed and why. Every operation runs entirely in your browser with no data sent to any server.
Schema Auto-Generator
Paste example JSON data below and click Generate Schema to produce a draft-07 JSON Schema. The generator analyzes each property's type, detects string formats (email, URI, date-time, UUID, IPv4), marks properties that appear in all examples as required, and recursively handles nested objects and arrays. Provide multiple examples separated by newlines to improve required property detection.
Example JSON Input
Schema Validator
Paste a JSON Schema in the left field and JSON data in the right field, then click Validate to check the data against the schema. The validator reports each error with the property path, expected type or constraint, and the actual value that failed. Use the schema generated above or paste your own.
Validate JSON Against Schema
Interactive Schema Builder
Build a schema property by property using the form below. Select the property name, type, and constraints, then click Add Property. The builder accumulates properties and generates the complete schema on the fly. This is useful for designing schemas from scratch when you do not have example data to auto-generate from.
Add Schema Properties
JSON Schema Type System
JSON Schema defines seven primitive types that map directly to JSON value types. Understanding the type system is the foundation of writing effective schemas because every validation keyword is type-specific. A "minimum" constraint only applies to numbers, a "minLength" constraint only applies to strings, and applying the wrong keyword to the wrong type produces silent validation failures where the constraint is ignored.
| Type | JSON Values | Key Constraints | Common Formats |
|---|---|---|---|
string | "hello" | minLength, maxLength, pattern | email, uri, date-time, uuid |
number | 3.14, -1.5 | minimum, maximum, multipleOf | - |
integer | 42, -7 | minimum, maximum, multipleOf | - |
boolean | true, false | const, enum | - |
array | [1, 2, 3] | items, minItems, maxItems, uniqueItems | - |
object | {"key": "value"} | properties, required, additionalProperties | - |
null | null | const | - |
Common Schema Patterns
Nullable Fields
A field that can be either a string or null requires a type array: {"type": ["string", "null"]}. This is the correct way to represent optional values that are present in the response but may have no value, which is different from the property being absent entirely. The "required" keyword controls whether the property must exist, while the type array controls what values it can hold when it does exist.
Enum Validation
The "enum" keyword restricts a value to a fixed set: {"type": "string", "enum": ["active", "inactive", "pending"]}. Enums work with any type and provide the tightest validation possible when you know all valid values at schema design time. For single-value constants, use "const" instead: {"const": "v1"} is cleaner than {"enum": ["v1"]}.
Conditional Schemas
Draft-07 introduced if-then-else for conditional validation. If a "type" field is "business", require a "taxId" property: {"if": {"properties": {"type": {"const": "business"}}}, "then": {"required": ["taxId"]}}. This replaces the complex oneOf/anyOf patterns that were necessary in draft-04 for conditional requirements and makes the schema intention much clearer to readers.
Reusable Definitions
Use "$defs" (or "definitions" in draft-07) to define reusable sub-schemas referenced by "$ref": {"$defs": {"address": {"type": "object", "properties": {"street": {"type": "string"}}}}, "properties": {"billing": {"$ref": "#/$defs/address"}, "shipping": {"$ref": "#/$defs/address"}}}. This eliminates duplication and ensures consistent validation across properties that share the same structure.
Validation Keywords Reference
Each validation keyword applies to specific types and controls a single constraint. Combining keywords creates compound validation rules. The most powerful combinations are: type + format for semantic string validation, type + minimum + maximum for bounded numbers, properties + required + additionalProperties: false for strict object shapes, and items + minItems + uniqueItems for constrained arrays.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"email": { "type": "string", "format": "email" },
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"age": { "type": "integer", "minimum": 0, "maximum": 150 },
"role": { "type": "string", "enum": ["admin", "user", "guest"] },
"tags": { "type": "array", "items": { "type": "string" },
"minItems": 0, "maxItems": 10, "uniqueItems": true },
"metadata": { "type": "object", "additionalProperties": true }
},
"required": ["id", "email", "name"],
"additionalProperties": false
}
Schema in API Workflows
JSON Schema integrates into every stage of the API lifecycle. During design, schemas define the API contract in OpenAPI specifications. During development, schemas generate TypeScript interfaces, Python dataclasses, and Go structs automatically. During testing, schemas power contract testing that verifies both request and response formats. During runtime, schemas validate incoming requests in API middleware, rejecting malformed data with structured error responses before your business logic executes.
The most impactful integration point is request validation middleware. A schema-validated API endpoint rejects invalid requests with a 400 response containing the specific validation errors, which improves security (blocks injection attempts), reliability (catches client bugs early), and developer experience (clear error messages tell the client exactly what to fix). The implementation cost is minimal: load schemas at startup, validate on each request, and return errors in a standard format.
Schema Design Best Practices
First, always set additionalProperties: false for request schemas. This prevents clients from sending unexpected properties that your code ignores today but might process incorrectly after a future refactor. For response schemas, leave additionalProperties: true (the default) so you can add new fields without breaking existing client validations. This asymmetry between request strictness and response flexibility is the key to forward-compatible API evolution.
Second, use description on every property. Descriptions become API documentation automatically when your schema is embedded in an OpenAPI spec. A schema with descriptions is self-documenting; a schema without descriptions requires a separate documentation effort that inevitably drifts out of sync. Third, prefer specific formats over generic types. {"type": "string", "format": "email"} is more useful than {"type": "string"} because it enables format-aware validation, generates better documentation, and gives code generators enough information to use email-specific types in client SDKs.
Fourth, version your schemas independently from your API version. A schema change that adds a new optional property is backward-compatible and does not require a new API version. A schema change that removes a property or changes a type is breaking and requires a version bump. Track schema changes in version control alongside your API code so that every deploy has a matching schema version, and use schema diffing tools to detect breaking changes in your CI pipeline.
Frequently Asked Questions
What is JSON Schema and why should I use it?
JSON Schema is a vocabulary that allows you to validate, annotate, and describe the structure of JSON data. It defines what properties are required, what data types are expected, value constraints like minimum and maximum, and string patterns like email or URL formats. You should use it because it catches data errors at the API boundary before they cause bugs in your application logic, generates documentation automatically, and enables code generation for client SDKs in any language.
How do you auto-generate a JSON Schema from example data?
Auto-generation analyzes example JSON data to infer the schema by examining each value's type, detecting patterns in strings (emails, URLs, dates, UUIDs), determining which properties appear consistently across multiple examples to mark as required, and inferring array item schemas from element types. The generated schema is a starting point that you should refine by adding descriptions, tightening constraints, and specifying additional validation rules that cannot be inferred from data alone.
What is the difference between JSON Schema draft-04, draft-07, and 2020-12?
Draft-04 is the oldest widely-used version with basic type validation and required properties. Draft-07 added if-then-else conditionals, const keyword, and readOnly/writeOnly annotations. Draft 2020-12 is the current standard adding prefixItems for tuple validation, dynamic references, and vocabulary extensibility. Most production APIs use draft-07 because it has the widest library support and covers most validation needs. Use 2020-12 for new projects if your toolchain supports it.
How do I validate API responses against a JSON Schema?
Validate API responses by loading the schema at application startup, then passing each response body through a schema validator library before processing. In Node.js use ajv, in Python use jsonschema, in Go use gojsonschema. For request validation, apply the schema in API middleware to reject malformed requests with a 400 response that includes the specific validation errors. For response validation, log violations in production rather than rejecting responses to avoid breaking clients when the schema and response diverge.
Can JSON Schema validate nested objects and arrays?
Yes, JSON Schema supports deep nesting through recursive property definitions. For nested objects, define a properties object within a property. For arrays, use the items keyword to define the schema for array elements, which can be objects with their own properties. You can also use minItems, maxItems, and uniqueItems to constrain array length and uniqueness. The additionalProperties keyword controls whether extra properties are allowed in objects, which is important for forward compatibility.