Glossary · Definition
JSON Schema
JSON Schema is a vocabulary for describing the structure of valid JSON: required fields, types, allowed values, nested-object shape. Use it for API request validation, OpenAPI docs, and structured data validation. Modern alternatives: TypeScript types (for compile time), Zod / Yup / Valibot (for runtime validation in JS).
Definition
JSON Schema is a vocabulary for describing the structure of valid JSON: required fields, types, allowed values, nested-object shape. Use it for API request validation, OpenAPI docs, and structured data validation. Modern alternatives: TypeScript types (for compile time), Zod / Yup / Valibot (for runtime validation in JS).
What it means
JSON Schema is itself written in JSON. Example: <code>{"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}, "age": {"type": "integer", "minimum": 0}}}</code> says “name is required string, age is non-negative integer.” The Draft 2020-12 spec is current; Draft 7 is widely supported. Validators exist in every major language (Ajv for JavaScript, jsonschema for Python, Pydantic for Python with auto-generation). OpenAPI specs use JSON Schema for request/response validation. Modern JS alternatives (Zod, Yup, Valibot) write validation in the language directly with auto-inferred TypeScript types — often more ergonomic than separate JSON Schema files.
Advertisement
Why it matters
Strict JSON catches syntax errors but not semantic errors. <code>{"userId": "abc"}</code> is valid JSON but invalid in an API expecting numeric ID. JSON Schema catches that. For REST/GraphQL APIs: documenting expected shape via OpenAPI lets clients verify contracts before deployment. For form validation: schema-based validation centralizes rules in one place. For data-pipeline ETL: schema validation catches malformed data before it propagates.
Example
Schema: <code>{"type": "object", "required": ["email"], "properties": {"email": {"type": "string", "format": "email"}}}</code>. Valid: <code>{"email": "a@b.com"}</code>. Invalid: <code>{"email": 42}</code> (wrong type), <code>{}</code> (missing required), <code>{"email": "not-an-email"}</code> (format violation).
Related free tools
Frequently asked questions
JSON Schema vs Zod vs Pydantic?
JSON Schema: language-neutral, written in JSON. Zod (JS/TS), Pydantic (Python), Joi (older JS): language-native, more ergonomic for that language but not portable. Modern apps often use Zod for runtime validation + auto-generate JSON Schema for OpenAPI docs.
What versions exist?
Drafts 4, 6, 7, 2019-09, 2020-12 (current). Most validators support Draft 7+. Differences are minor for most use cases.
Can I auto-generate schemas from data?
Yes — tools like quicktype.io infer schema from sample JSON. Useful starting point but typically needs manual refinement (sample data may not capture edge cases or required fields).
Related terms
- DefinitionJSON vs JSON5JSON (RFC 8259) is the strict standard: keys must use double quotes, no comments, no trailing commas. JSON5 is a developer-friendly superset: allows unquoted keys, single quotes, comments, and trailing commas. Use JSON for APIs and data interchange; JSON5 for config files where humans edit.
- DefinitionJSON syntax rulesJSON syntax: 6 value types (string, number, true, false, null, object, array). Strings use double quotes ONLY. Object keys must be quoted strings. No trailing commas, no comments, no single quotes. Common errors: missing commas, unquoted keys, trailing commas after last item.