What Is JSON Schema?

JSON lets you write data in almost any shape. That freedom is handy, but it also causes bugs. A field goes missing. A number arrives as text. A list shows up where you expected one value.

JSON Schema is a way to write down the shape your data should have. A program can then check real data against it. This guide explains how it works, with a small example you can try.

Quick answer: JSON Schema is a standard way to describe what JSON data should look like. It says which fields the data must have, what type each field is, and which values are allowed. A validator then checks your data against it. It lists anything that doesn’t match.

What problem does JSON Schema solve?

Say your app expects a user like this:

{ "name": "Ada", "age": 36 }

But one day it receives this:

{ "age": "thirty-six" }

This is valid JSON. The syntax is fine, so a normal parser (a program that reads JSON) accepts it. But the data is wrong: the name is missing, and the age is text instead of a number. Your app may crash later, far away from the real cause.

There are two different checks here. A JSON validator checks the syntax: commas, quotes and brackets. JSON Schema checks the content: the right fields, with the right types and values. You can read more about both in how to validate JSON.

A simple JSON Schema example

A schema is itself written in JSON. Here is one for a user:

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

In plain words, this says: “The data must be an object. name is text with at least one character. age is a whole number, zero or more. email is text that looks like an email address. tags is a list of text values. name and email must always be there.”

The main keywords, explained

The words in a schema, like type and required, are called keywords. These are the ones you will use most:

  • $schema says which version of JSON Schema the file uses.
  • type sets the kind of value: string, number, integer, boolean, object, array or null.
  • properties lists the fields of an object and gives each one its own rules.
  • required lists the fields that must be present.
  • items sets the rules for every item in an array.
  • minimum and maximum limit numbers. minLength and maxLength limit the length of text.
  • enum lists the only values allowed, such as ["small", "medium", "large"].
  • format checks common text patterns, such as email, date or uuid.
  • additionalProperties set to false rejects any field you didn’t list.

Two rules surprise many beginners. First, listing a field under properties does not make it required. Only required does that. Second, extra fields are allowed by default. Add "additionalProperties": false if you want to block them.

How to validate JSON against a schema

Let’s check this data against the schema above:

{
  "name": "Ada",
  "age": -5,
  "tags": ["admin", 42]
}

A validator would report three problems:

  • age is below the minimum of 0.
  • The second item in tags is a number, not a string.
  • email is missing, but it is required.

To try it yourself, open the JSON Schema Validator. Paste your data on the left and your schema on the right. It checks as you type. Each error shows the path to the wrong value and what was expected.

It also catches a problem that is easy to miss. Say you spell a keyword wrong, such as “requred”. Validators just ignore it, so the rule stops working without any warning. JSONPilot shows unknown keywords as warnings, so you can find and fix these typos.

In your own code, you use a validator library. In JavaScript, Ajv is the most widely used one. In Python, the jsonschema package works like this:

from jsonschema import validate, ValidationError

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

Note that some libraries skip format checks unless you turn them on. Check your library’s docs if bad emails get through.

What are JSON Schema drafts?

JSON Schema has had several versions, called drafts. The $schema line tells the validator which one your schema uses. You will see draft-07 and 2020-12 most often. The JSONPilot validator supports draft-04, 06, 07, 2019-09 and 2020-12. If a schema has no $schema line, it uses draft-07.

How to generate a schema from sample data

Writing a schema by hand takes time. A faster way is to start from an example. Paste a sample into the JSON Schema Generator. It works out the types and required fields for you. It also handles nested objects, arrays and common formats.

For arrays of objects, it looks at every item. A key found in all items becomes required. A key found in only some items becomes optional. You can choose 2020-12 or draft-07, and whether to allow extra fields.

A generated schema is a starting point. It can’t know the special rules of your app. Add things like enum, minimum and descriptions afterwards. Then test it with the validator.

Where is JSON Schema used?

  • APIs. OpenAPI, a popular way to document APIs, describes request and response data with JSON Schema.
  • Code editors. VS Code uses schemas to give autocomplete and warnings in files like package.json.
  • AI tools. Many AI APIs accept a JSON Schema that describes the output you want back. Even so, you should still check the result. See how to fix JSON from an LLM.
  • Config files. A schema can check a config file before your app starts.

Common questions

Is JSON Schema the same as JSON?

No. JSON is the data format. A JSON Schema is a JSON document that describes what other JSON data should look like.

Does JSON Schema check for syntax errors?

No. Your data must be valid JSON before a schema can check it. Fix syntax problems first with a JSON validator.

Which JSON Schema draft should I use?

For a new project, use 2020-12 if your tools support it. If they don’t, draft-07 is a safe and widely supported choice.

Can I create a JSON Schema automatically?

Yes. A schema generator can build one from sample data. Review it and add your own rules before you rely on it.