โ† Home ยท Blog

JSON Schema Explained โ€” Validate Your API Data

Published: May 28, 2025 ยท Last updated: September 7, 2026 ยท 6 min read ยท By JSON Suite Team

How do you ensure that JSON data from an API, a form submission, or a config file has the correct structure? You write a JSON Schema. Think of it as a contract that describes exactly what valid data looks like โ€” what fields are required, what types they should be, and what values are acceptable. If you're new to JSON, first check out our guide on what JSON is.

What is JSON Schema?

JSON Schema is itself written in JSON. It describes the expected structure of a JSON document โ€” similar to how a TypeScript interface defines the shape of an object, or how an XML DTD/XSD validates XML documents. JSON Schema is an open standard (currently at Draft 2020-12) supported by validators in every major language.

A Simple Example

Given this JSON data:

{"name": "Alice", "age": 30, "email": "alice@example.com"}

A schema that validates it:

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "name": {"type": "string", "minLength": 1}, "age": {"type": "integer", "minimum": 0, "maximum": 150}, "email": {"type": "string", "format": "email"} }, "required": ["name", "email"] }

This schema says: the data must be an object with a required name (non-empty string) and email (valid email format), plus an optional age (integer between 0 and 150).

Schema Keywords Reference

KeywordPurposeExample
typeExpected data type"string", "number", "object", "array"
propertiesDefine object fields{"name": {"type": "string"}}
requiredFields that must exist["name", "email"]
itemsSchema for array items{"type": "string"}
minimum/maximumNumber range"minimum": 0
minLength/maxLengthString length"maxLength": 100
patternRegex for strings"pattern": "^[A-Z]"
enumAllowed values"enum": ["active", "inactive"]

Nested Objects and Arrays

Schemas can validate deeply nested structures:

{ "type": "object", "properties": { "name": {"type": "string"}, "address": { "type": "object", "properties": { "city": {"type": "string"}, "zip": {"type": "string", "pattern": "^[0-9]{5}$"} }, "required": ["city"] }, "tags": { "type": "array", "items": {"type": "string"}, "minItems": 1 } } }

Where JSON Schema is Used

  • OpenAPI/Swagger โ€” API documentation uses JSON Schema to define request/response bodies
  • Form validation โ€” Libraries like Ajv (JavaScript) validate form data against schemas
  • Configuration files โ€” VS Code uses schemas to validate settings.json, package.json, etc.
  • Data contracts โ€” Microservices agree on data formats using shared schemas
  • Code generation โ€” Generate TypeScript types, validation code, or documentation from schemas
  • Testing โ€” Validate API responses in automated tests

Validation Libraries

  • JavaScript: Ajv (fastest), Zod, Yup
  • Python: jsonschema, pydantic
  • Java: everit-org/json-schema
  • Go: xeipuuv/gojsonschema
  • .NET: Newtonsoft.Json.Schema

Learn More About JSON

JSON Schema works hand-in-hand with other JSON concepts. Learn about JSON API best practices for designing well-structured APIs, explore common JSON errors to avoid, and understand how to format JSON for better readability. For API security, check out our guide on JWT authentication.

Generate a Schema Automatically

Don't want to write schemas by hand? Our JSON Schema Generator creates a schema from sample data. Paste your JSON, click Generate, and get a complete schema instantly. You can then refine it by adding constraints, patterns, and descriptions.

Related Tools

Work with JSON Schema using these free tools:

More Articles

What is JWT โ€” Token Authentication

JSON API Best Practices

10+ Common JSON Errors

What is JSON โ€” A Complete Guide