JSON Schema in One Sentence

A JSON Schema is itself a JSON document that sets the rules for other JSON. Think of it as a form with required fields: the data either fits the rules and is valid, or it breaks a rule and you get a clear error that says where and why.

A Complete Example

Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "type": "object",
  "properties": {
    "id":    { "type": "integer", "minimum": 1 },
    "name":  { "type": "string", "minLength": 2 },
    "email": { "type": "string", "format": "email" },
    "role":  { "enum": ["admin", "editor", "viewer"] },
    "tags":  { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
  },
  "required": ["id", "name", "email"],
  "additionalProperties": false
}

This data passes every rule:

Valid
{ "id": 1, "name": "Emily Clark", "email": "emily@example.com", "role": "admin", "tags": ["json"] }

This data breaks five rules:

Invalid
{ "id": 0, "name": "E", "email": "not-an-email", "role": "owner", "nickname": "Em" }
Errors
(root)  has an extra field "nickname" that is not allowed
/id     must be >= 1
/name   must NOT have fewer than 2 characters
/email  must match format "email"
/role   must be one of: "admin", "editor", "viewer"

Paste both into the JSON Schema Validator to see these errors yourself.

The Keywords You Will Use Most

KeywordWhat it checks
typestring, number, integer, boolean, object, array or null
propertiesThe rules for each field of an object
requiredFields that must be present
additionalPropertiesWhether fields not listed are allowed
minimum / maximumLimits for numbers
minLength / maxLengthLimits for text length
patternA regular expression the text must match
formatemail, date, date-time, uri, uuid, ipv4 and more
enumA fixed list of allowed values
items / minItems / uniqueItemsRules for arrays

Optional and Nullable Fields

A field that is not in required may be left out. A field that may be present but empty should allow null as a type:

Schema
"manager": { "type": ["string", "null"] }

Reusing Rules with $defs and $ref

When the same shape appears in several places, define it once and point to it:

Schema
{
  "$defs": {
    "address": {
      "type": "object",
      "properties": { "city": { "type": "string" }, "zip": { "type": "string" } },
      "required": ["city"]
    }
  },
  "type": "object",
  "properties": {
    "home": { "$ref": "#/$defs/address" },
    "work": { "$ref": "#/$defs/address" }
  }
}

Which Version Should I Use?

The $schema line says which version of JSON Schema you follow. Use draft 2020-12 for new work; it is the current version and is used by OpenAPI 3.1. Many existing projects still use draft-07, which is also widely supported. Older schemas use definitions where newer ones use $defs.

Where JSON Schema Is Used

  • APIs: check request bodies before your code runs, and describe them in OpenAPI docs.
  • Config files: editors like VS Code use schemas to autocomplete and check files such as package.json and tsconfig.json.
  • Forms: libraries can build forms and their validation from a schema.
  • Data pipelines: reject bad records before they reach a database.

Validate JSON in Code

JavaScript (Ajv)
import Ajv from 'ajv/dist/2020'
import addFormats from 'ajv-formats'

const ajv = new Ajv({ allErrors: true })
addFormats(ajv)
const validate = ajv.compile(schema)

if (!validate(data)) console.log(validate.errors)
Python (jsonschema)
from jsonschema import Draft202012Validator

validator = Draft202012Validator(schema)
for error in validator.iter_errors(data):
    print(error.json_path, error.message)

Get Started in a Minute

You do not have to write a schema by hand. Paste a sample of your data into the JSON Schema Generator, add the extra rules you need, then test real data with the JSON Schema Validator.

Frequently Asked Questions

What is JSON Schema used for?

JSON Schema describes the structure of valid JSON: required fields, value types and allowed values. It is used to validate API requests, document APIs in OpenAPI, check config files in editors and build forms.

Is JSON Schema the same as JSON?

A JSON Schema is written in JSON, but it is a set of rules for other JSON documents. The data is the JSON; the schema describes what that data must look like.

Which JSON Schema version should I use?

Use draft 2020-12 for new projects. Draft-07 is older but still widely supported. The $schema keyword at the top says which version a schema uses.

How do I create a JSON Schema quickly?

Paste a sample of your JSON into our free JSON Schema Generator, then add extra rules like minLength, format or enum, and test it with the JSON Schema Validator.

Related Tools