What is JSON Schema?

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It describes the structure, constraints, and documentation of JSON data, enabling automated validation and documentation generation.

Quick Facts

Full NameJSON Schema Specification
Created2009 (first draft)
SpecificationOfficial Specification

How It Works

JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. It can validate data types, required properties, string patterns, numeric ranges, array lengths, and more. JSON Schema is written in JSON itself, making it easy to understand and generate. It supports features like $ref for reusable definitions, allOf/anyOf/oneOf for composition, and conditional validation with if/then/else. JSON Schema is widely used in API documentation (OpenAPI), configuration validation, and form generation. JSON Schema Draft 2020-12 introduced several improvements including $dynamicRef for recursive schemas, prefixItems replacing tuple validation, and improved vocabulary system for extensibility. The $schema keyword should specify the draft version for compatibility.

Key Characteristics

  • Written in JSON format
  • Validates data types, formats, and constraints
  • Supports $ref for reusable definitions
  • Composition with allOf, anyOf, oneOf
  • Conditional validation with if/then/else
  • Used in OpenAPI/Swagger specifications

Common Use Cases

  1. API request/response validation
  2. Configuration file validation
  3. Form generation and validation
  4. Documentation generation
  5. Data interchange contracts

Example

loading...
Loading code...

Frequently Asked Questions

What is JSON Schema used for?

JSON Schema is used to validate JSON data structure, define API contracts, generate documentation, create forms, and ensure data consistency. It specifies what properties are required, their data types, formats, and constraints, enabling automated validation in applications and APIs.

What is the difference between JSON and JSON Schema?

JSON is a data format for storing and exchanging data (e.g., {"name": "John"}). JSON Schema is a specification written in JSON that describes what valid JSON data should look like. JSON Schema defines the rules; JSON is the data that follows those rules.

How do I validate JSON against a JSON Schema?

Use a JSON Schema validator library for your programming language (e.g., ajv for JavaScript, jsonschema for Python, json-schema for Ruby). These libraries take your schema and data as input and return validation results, including any errors found.

What are the different JSON Schema draft versions?

Major versions include Draft-04, Draft-06, Draft-07, Draft 2019-09, and Draft 2020-12. Each version adds features and improvements. Draft 2020-12 is the latest, introducing $dynamicRef and prefixItems. Always specify the $schema keyword to indicate which version your schema uses.

How do I reference another schema using $ref?

$ref allows you to reference and reuse schema definitions. Use $ref with a JSON Pointer (e.g., "$ref": "#/$defs/address") for internal references or a URI for external schemas. This promotes DRY principles and makes schemas more maintainable and modular.

Related Tools

Related Terms

Related Articles