← Back to Blog
Tutorial6 min read

Introduction to JSON Schema Validation

Published on January 15, 2026

JSON Schema is a powerful tool for validating the structure and content of JSON data. In this guide, we'll learn how to create and use JSON Schemas to ensure data integrity.

What is JSON Schema?

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It describes the structure, required properties, data types, and constraints of your JSON data.

Basic Schema Structure

A simple JSON Schema looks like this:

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

Data Types

JSON Schema supports these data types:

  • string - Text values
  • number - Any numeric value
  • integer - Whole numbers only
  • boolean - true or false
  • array - Ordered list of values
  • object - Key-value pairs
  • null - Null value

String Validation

You can validate strings with various constraints:

{
  "type": "string",
  "minLength": 1,
  "maxLength": 100,
  "pattern": "^[A-Za-z]+$",
  "format": "email"
}

Number Validation

{
  "type": "number",
  "minimum": 0,
  "maximum": 100,
  "multipleOf": 5
}

Array Validation

{
  "type": "array",
  "items": { "type": "string" },
  "minItems": 1,
  "maxItems": 10,
  "uniqueItems": true
}

Nested Objects

{
  "type": "object",
  "properties": {
    "address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" },
        "zipCode": { "type": "string", "pattern": "^[0-9]{5}$" }
      },
      "required": ["street", "city"]
    }
  }
}

Using Enums

Restrict values to a specific set:

{
  "type": "string",
  "enum": ["pending", "active", "completed", "cancelled"]
}

Conditional Validation

Apply different rules based on conditions:

{
  "type": "object",
  "properties": {
    "type": { "enum": ["personal", "business"] },
    "companyName": { "type": "string" }
  },
  "if": {
    "properties": { "type": { "const": "business" } }
  },
  "then": {
    "required": ["companyName"]
  }
}

JavaScript Validation Libraries

Popular libraries for JSON Schema validation:

  • Ajv - Fast and feature-complete validator
  • Joi - Object schema validation
  • Yup - Schema builder with TypeScript support
  • Zod - TypeScript-first schema validation

Benefits of JSON Schema

  • Catch errors early before processing data
  • Document your API contract
  • Generate TypeScript types from schemas
  • Auto-generate documentation
  • Validate user input on both client and server

Conclusion

JSON Schema is an essential tool for maintaining data quality in your applications. Start with simple schemas and gradually add more constraints as your needs evolve.

Try Our JSON Formatter

Format, validate, and minify your JSON data instantly - no sign-up required!

Open JSON Formatter
simple json formatter