What Is JSON Schema and How to Validate Your Data

JSON, by itself, doesn’t enforce any fixed structure: an object can have any key, with any type of value, and a technically valid JSON can be completely wrong from your application’s point of view (missing required fields, wrong types, etc.). JSON Schema solves this: it’s a specification for describing and validating the expected structure of a JSON document.

The problem JSON Schema solves

Consider this JSON:

{
  "name": "Ana",
  "age": "thirty"
}

This is perfectly valid JSON from a syntax standpoint. But if your application expects age to be a number, not a string, this document will cause problems further down in your code, without the JSON parser having detected anything wrong. JSON Schema lets you define these rules explicitly, and then validate documents against them before processing them.

A basic schema example

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    }
  },
  "required": ["name", "age"]
}

This schema describes an object that must have:

  • A name key, of type string
  • An age key, of type integer, with a minimum value of 0
  • Both keys are required (required)

If you validate the earlier JSON example (with age as a string) against this schema, the validation fails, pointing to exactly which rule was violated.

Available types and validations

JSON Schema covers JSON’s basic types (string, number, integer, boolean, object, array, null), and each type has its own additional validation rules.

Strings:

{
  "type": "string",
  "minLength": 3,
  "maxLength": 50,
  "pattern": "^[A-Za-z]+$"
}

Numbers:

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

Arrays:

{
  "type": "array",
  "items": {
    "type": "string"
  },
  "minItems": 1
}

This defines an array where every item must be a string, and the array must have at least 1 item.

Enums (fixed allowed values):

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

Nested structures

Schemas can validate objects inside objects, the same way JSON allows nesting.

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "address": {
      "type": "object",
      "properties": {
        "city": { "type": "string" },
        "zip": { "type": "string" }
      },
      "required": ["city"]
    }
  }
}

Where JSON Schema is used in practice

  • API payload validation: making sure an incoming request has the expected structure before processing it
  • API documentation: tools like OpenAPI (Swagger) use JSON Schema to describe request/response formats
  • Code generation: some tools generate types (TypeScript, Java classes, etc.) automatically from a JSON Schema
  • Configuration file validation: code editors like VS Code use JSON Schema to provide autocomplete and real-time validation in .json files (like package.json)

How to validate in practice

Most languages have specific libraries for validating JSON against a schema:

  • JavaScript: ajv, one of the most widely used and fastest libraries
  • Python: jsonschema
  • Java: everit-json-schema or networknt/json-schema-validator
  • PHP: justinrainbow/json-schema

The general pattern is the same across languages: you load the schema, load the JSON document, and call a validation function that returns whether it passed or not (usually with a list of specific errors when it fails).

Quick checklist

  • Did you define which fields are truly required (required) versus optional?
  • Did you specify the correct types for each field, including relevant constraints (minimum, maxLength, etc.)?
  • Did you consider using enum for fields that only accept a fixed set of values?
  • Are you validating external input data (API, file upload) before processing it in your application?

Before creating a schema, it’s worth confirming your example documents are already valid JSON. Paste them into JSON Validator to check the syntax quickly.