Understand JSON Schema

What a JSON Schema is, what it does and does not change in your data, how it differs from JSON-LD and database schemas, and when plain JSON is enough.

A JSON Schema is a JSON document that describes what another JSON document is allowed to look like. It states which fields must be present, what type each value should be, and which limits apply, such as a minimum number or a minimum string length. A validator compares a JSON document against the schema and reports whether it conforms and, if not, where it fails. This review explains what that process changes, what it leaves untouched, and when a schema is worth the extra effort.

Is JSON Schema suitable for your intended use?

The main benefit is a single, machine-checkable contract for the shape of your data. The same schema can reject malformed API requests, check configuration files before deployment, and document the expected structure for other developers. Some editors and code generators can also read schemas, but support varies by tool, so check the documentation of the specific tool you plan to use.

The decisive tradeoff is compatibility and complexity. JSON Schema has been published in several drafts, including draft-04, draft-06, draft-07, 2019-09 and 2020-12, and keyword behavior differs between them. A validator built for one draft may interpret or ignore keywords from another. Schemas that use composition keywords such as oneOf, anyOf, allOf and $ref can become hard to read and debug. Some business rules, especially rules that compare values across several fields, are awkward or impossible to express in a schema and still need ordinary code.

Plain JSON without a schema is often the better choice when:

  • The data is used once, for example by a throwaway script or a quick prototype.
  • One program both writes and reads the data, and that program already checks the values it needs.
  • The data structure is still changing quickly, so maintaining a schema would mostly mean rewriting it.
  • The people reading the rules are not developers, in which case a short written description serves them better than a schema file.

What does a JSON Schema actually change?

Validation does not change your data. A conforming validator reads the JSON document and the schema, then returns a result: valid or invalid, usually with error details. The stored characters of the document stay the same. This sets validation apart from transformations that rewrite text.

Three nearby operations are easy to confuse with validation:

  • Formatting or pretty-printing changes only whitespace and indentation. It confirms that the text is syntactically valid JSON but says nothing about whether the fields and values are correct.
  • Minifying removes whitespace. Like formatting, it changes presentation but not meaning.
  • Default filling and type coercion are optional features of some validators. In the specification, keywords such as default are annotations, so the schema records a suggested value but does not insert it. If a library inserts defaults or converts the string "36" into the number 36, that is a library behavior that changes your data, and you should confirm it is enabled on purpose.

Here is a small schema for a user record. It requires a non-empty name and a non-negative whole-number age, allows an optional email field, and rejects any property it does not list. In practice, you would also add a $schema keyword that identifies which draft the schema follows.

{
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "age"],
  "additionalProperties": false
}

This document passes, because both required fields are present and valid. The email field may be left out because it is not listed as required:

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

This document fails for three separate reasons. The name is an empty string, which breaks minLength. The age is the string "36" rather than an integer. The nickname property is not listed, so additionalProperties rejects it.

{ "name": "", "age": "36", "nickname": "A" }

The word "schema" also has other meanings, and search results often mix them up:

Term | What it describes | Typical clues
JSON Schema | Rules for the structure and values of JSON data | Keywords such as type, properties, required, $schema
Schema.org markup in JSON-LD | Structured data that describes web page content for search engines | Keys such as @context and @type, with types like Product or Article
Database schema | Tables, columns and constraints in a database | SQL statements such as CREATE TABLE, or migration files
OpenAPI schema object | Request and response shapes in an API description | Appears inside an OpenAPI document; version 3.1 is aligned with JSON Schema 2020-12

Will a schema stay readable and usable after copying or sharing?

A schema is plain JSON text, so it copies and pastes like any other text. The common failures come from the text being changed along the way:

  • Word processors and some chat or document tools replace straight quotes with curly quotes. JSON accepts only straight double quotes, so the pasted schema stops parsing.
  • Standard JSON does not allow comments or trailing commas. A schema that worked in a tool accepting a relaxed JSON variant may fail in a strict parser.
  • If the $schema keyword is missing, the receiving validator may assume a different draft and apply keywords differently.
  • The format keyword, as in "format": "email", is treated as an annotation by default in draft 2020-12. Some validators enforce it and others do not unless you configure them to, so the same document can pass in one environment and fail in another.

For accessibility, how readable a schema is depends mostly on the editor or viewer, not on the schema itself. This review has no tested evidence on how particular screen readers handle deeply nested JSON or validator error output, so no claim is made about specific assistive technologies. In general, a validator message that points to the failing location and explains the rule in words is easier to act on than a raw keyword name.

When non-developers need to understand the rules, give them a plain-text equivalent, such as a short list of required fields, allowed values and limits. The title and description keywords inside a schema can hold this explanation next to each rule, which helps keep the written version in sync with the machine-readable one.

Before you paste data containing personal or confidential information into an online validator or formatter, read that service's documented privacy and retention policy. A clean interface or reassuring wording does not show how the service handles submitted data. For sensitive data, a validator library running on your own machine avoids this question entirely.

How to recognize JSON Schema and find tools that work with it

A file is probably a JSON Schema if its top level contains keywords like type, properties, required, items or $schema, and if its values describe rules rather than real data. A file full of actual names, prices or IDs is a JSON instance, the data that gets validated. Files with @context and @type are almost always JSON-LD structured data, not JSON Schema.

No specific tools were verified for this review, so none are named here. These search terms are useful starting points:

  • "JSON Schema validator" together with your programming language, to find a library you can run locally
  • "JSON Schema draft 2020-12" or the draft your project uses, to find the official specification and keyword reference
  • "generate JSON Schema from JSON", to find tools that draft a starting schema from sample data, which you then tighten by hand
  • "OpenAPI schema object", if your schema lives inside an API description

Whichever tool you choose, check which drafts it supports and whether it enforces format, fills defaults or coerces types. Those three behaviors are the most common reasons the same schema gives different results in different places.