Skip to content
HandyKit

JSON Schema Explained, with Examples

· 3 min read

JSON Schema is a way to describe what a JSON document should contain. You write the description once, then check any document against it. A schema can say which fields must be present, what type each value has and which values are allowed.

This guide explains the main keywords with one worked example. Paste the examples into the JSON Schema Validator to see the results as you read.

Syntax checking and schema validation

These are two different checks. Syntax checking confirms that text is well-formed JSON, with matching brackets and quoted keys. Schema validation starts after that: it checks that well-formed JSON has the right content, such as required fields, types, ranges and formats.

If you only need a syntax check, the JSON Validator is simpler.

A schema is JSON

A schema is itself a JSON document, so there is no new file format to learn, only a set of keywords. These are the ones you will use most:

  • type sets the kind of value: string, number, integer, boolean, object, array or null.
  • required lists the property names that must be present.
  • properties describes each named property of an object.
  • minLength sets the shortest allowed string, and minimum the lowest allowed number.
  • format checks that a string has a known shape, such as email, date-time or uri.
  • additionalProperties: false rejects any property not listed in properties.
  • enum limits a value to a fixed list.
  • items describes the elements of an array.
  • $defs and $ref let you reuse parts of a schema.

A worked example

This schema describes a person with a name, an email address and an optional age:

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

The document must be an object with name and email. The name must be a string of at least one character, and the email must look like an email address. The age is not in required, so it may be left out, but if it is there it must be a whole number of 0 or more. Because additionalProperties is false, no other properties are allowed.

This document passes every rule:

{ "name": "Ada", "email": "ada@example.com", "age": 36 }

Reading the results for an invalid document

This document breaks several rules at once:

{ "name": "", "email": "not-an-email", "age": -3, "admin": true }

The validator lists every problem, not just the first, each with its location, the keyword that failed and a message:

LocationKeywordMessage
$.nameminLengthString is too short (0 < 1).
$.emailformatString does not match format "email".
$.ageminimum-3 is less than 0.
$additionalPropertiesProperty "admin" does not match additional properties schema.
$.adminfalseThis property isn’t allowed by the schema.

The first three rows are separate problems: the name is empty, the email is not an email address and the age is below zero.

The last two rows are the same problem reported twice. admin is not listed in properties, so it is reported once at the object ($) and once at the property itself ($.admin). Removing admin clears both.

Reusing parts with $defs and $ref

Large schemas often repeat a shape, such as an address. Define it once under $defs, then point to it with $ref and a path such as #/$defs/address.

A $ref only works within the same schema: references to other files or URLs are not fetched. If you need a piece of another schema, copy it into $defs.

Choosing a draft

JSON Schema has several versions, called drafts, and the $schema keyword says which one a schema uses. The example above uses Draft 2020-12.

The validator supports Draft 2020-12, 2019-09, Draft 7 and Draft 4. It reads $schema to choose, falls back to 2020-12 when it is missing, and lets you pick a draft yourself.

Where schemas are useful

  • Checking API requests and responses.
  • Checking config files and test fixtures.
  • Documenting a format, since the schema states exactly what is allowed.

The validator runs in your browser, so your schema and document are not uploaded.

Try it

Paste the example schema and the invalid document into the JSON Schema Validator, then fix the document one problem at a time.

Tools in this guide

  • JSON Schema Validator

    Check a JSON document against a JSON Schema. Every problem is listed with where it is, what is wrong and which rule failed, as you type.

  • JSON Validator

    Paste or open JSON to check it as you type. Errors show their line, column and a suggested fix; valid JSON gets a summary of its structure.

  • · 4 min read

    Common JSON Errors and How to Fix Them

    Trailing commas, single quotes, comments, True and None: the JSON syntax errors developers hit most often, what each error message means, and how to fix it.

  • · 2 min read

    Minify JSON: What It Does and When to Use It

    What minifying JSON removes, what it never changes, when it is worth doing and when it is not, and why sorting keys gives stable output you can compare or hash.

  • · 2 min read

    How to Compare Two JSON Files

    Find what changed between two JSON documents, ignoring key order and formatting. A worked example, how to read JSONPath, and how arrays and numbers compare.