elefcode
← All guides

Web & APIs · 5 min read

JSON Schema Explained: Validating JSON Data

JSON Schema is a JSON document that describes what other JSON documents should look like. It is used to validate API requests, power editor autocomplete for config files, generate forms, and document data contracts between teams.

Try it yourself with the related tool.

Preview a JSON Schema form →

Advertisement

A first schema

A schema for a user might say "type": "object", list "properties" such as name with "type": "string" and age with "type": "integer", "minimum": 0, and declare "required": ["name"]. A validator checks a document against it and reports each problem with a path to the offending value.

The keywords you will use most

Strings take minLength, maxLength, pattern, and format (such as email or date-time). Numbers take minimum, maximum, and multipleOf. Arrays use items, minItems, and uniqueItems. enum restricts a value to a fixed list, and additionalProperties: false rejects unknown keys — useful for catching typos.

Reuse and composition

Define shared pieces under $defs and reference them with $ref. Combine schemas with allOf (must match all), anyOf (at least one), and oneOf (exactly one). if / then / else handles conditional rules, such as requiring a postcode only when the country is set.

Schemas that build forms

Because a schema already knows each field's type, label, limits, and allowed values, libraries can render a form from it and validate input with the same rules the server uses. Add title, description, and default to each property to make the generated form friendly.

Related guides