JSON Schema Explained: How to Define and Validate Data Structures

JSON Schema is a vocabulary for annotating and validating JSON documents. Think of it as a contract that describes exactly what shape your data must be.

When you are building an API, processing data files, or working with configuration, you often need to enforce that incoming JSON has the right structure. JSON Schema is the standard tool for this.

A JSON Schema is itself a JSON document that describes the expected structure of another JSON document โ€” the types of values, which fields are required, allowed ranges, patterns, and more.

A Simple Example

Here is a JSON document describing a user:

{
  "id": 42,
  "username": "alice_dev",
  "email": "alice@example.com",
  "isActive": true
}

And here is a JSON Schema that validates it:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["id", "username", "email"],
  "properties": {
    "id":       { "type": "integer", "minimum": 1 },
    "username": { "type": "string",  "minLength": 3 },
    "email":    { "type": "string",  "format": "email" },
    "isActive": { "type": "boolean" }
  },
  "additionalProperties": false
}

This schema says: the object must have id, username, and email. The id must be a positive integer, username at least 3 chars, email must look like an email address. No extra fields are allowed.

Core JSON Schema Keywords

type

Specifies the expected data type. Valid values: string, number, integer, boolean, array, object, null.

{ "type": "string" }      // only strings pass
{ "type": "integer" }     // only whole numbers
{ "type": ["string", "null"] }  // string or null

required

An array of property names that must be present in the object:

{
  "type": "object",
  "required": ["name", "email"]
}

properties

Defines schemas for individual object properties. Each key is a property name and its value is a nested schema:

{
  "properties": {
    "age":  { "type": "integer", "minimum": 0, "maximum": 120 },
    "name": { "type": "string" }
  }
}

String Constraints

KeywordDescriptionExample
minLengthMinimum string length"minLength": 3
maxLengthMaximum string length"maxLength": 100
patternRegex the string must match"pattern": "^[a-z]+"
formatNamed format like email, date, uri"format": "date"
enumValue must be one of a fixed set"enum": ["red", "green", "blue"]

Number Constraints

KeywordDescription
minimum / maximumInclusive range boundaries
exclusiveMinimum / exclusiveMaximumExclusive range boundaries
multipleOfValue must be a multiple of this number

Array Constraints

{
  "type": "array",
  "items": { "type": "string" },   // all items must be strings
  "minItems": 1,                     // at least one item
  "maxItems": 10,                    // at most ten items
  "uniqueItems": true                // no duplicate values
}

Combining Schemas

anyOf โ€” valid if it matches any of the listed schemas

{
  "anyOf": [
    { "type": "string" },
    { "type": "number" }
  ]
}

allOf โ€” valid only if it matches all listed schemas

{
  "allOf": [
    { "type": "object" },
    { "required": ["id"] }
  ]
}

oneOf โ€” valid if it matches exactly one of the listed schemas

{
  "oneOf": [
    { "type": "integer" },
    { "type": "string", "format": "uuid" }
  ]
}

Using JSON Schema in Practice

Python with jsonschema

pip install jsonschema
import jsonschema
import json

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

data = {"name": "Alice", "age": 30}

try:
    jsonschema.validate(instance=data, schema=schema)
    print("Valid!")
except jsonschema.ValidationError as e:
    print(f"Invalid: {e.message}")

JavaScript with Ajv

npm install ajv
const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  type: 'object',
  required: ['name', 'age'],
  properties: {
    name: { type: 'string' },
    age:  { type: 'integer', minimum: 0 }
  }
};

const validate = ajv.compile(schema);
const data = { name: 'Alice', age: 30 };

if (validate(data)) {
  console.log('Valid!');
} else {
  console.log(validate.errors);
}

JSON Schema and Excel Conversion

When converting JSON to Excel, having a schema for your data helps in several ways:

  • Column ordering โ€” the schema's properties order can define the column order in the output spreadsheet
  • Type enforcement โ€” knowing that a field is an integer prevents it from being formatted as text in Excel
  • Documentation โ€” the schema serves as documentation for what each column means
  • Validation before conversion โ€” reject records that do not match the schema instead of producing a corrupted spreadsheet
Tip: Many API documentation tools (like Swagger/OpenAPI) use JSON Schema internally. If you are working with an API, check its OpenAPI spec โ€” it likely already has schemas for all the response objects you are converting.

Ready to Convert Your JSON?

Our free tool handles any valid JSON โ€” no schema required.

Convert JSON to Excel Now โ†’