← All Articles

JSON Schema Validation: A Complete Tutorial

March 2026 · 8 min read

As APIs grow more complex, how do you ensure data structure and content match expectations? JSON Schema provides a standardized way to describe and validate the structure of JSON data. This tutorial covers everything from basic concepts to advanced usage.

What is JSON Schema?

JSON Schema is a specification for describing the structure of JSON data. It is itself a JSON document that defines what structure, data types, and constraints another JSON document should have.

Key use cases include:

Essential JSON Schema Keywords

JSON Schema uses a set of keywords to define data structure. Here are the most commonly used ones:

KeywordPurposeExample Value
typeDefine data type"string", "number", "object"
propertiesDefine object propertiesSchema for each property
requiredRequired fields list["name", "email"]
minLengthMinimum string length1
maxLengthMaximum string length255
minimumMinimum number value0
maximumMaximum number value100
patternRegex validation"^[a-z]+$"
enumAllowed values["active", "inactive"]
itemsArray element schemaSchema definition for elements

Advanced Validation Features

1. Conditional Validation

JSON Schema supports conditional logic using if, then, and else keywords. For example, when type is "company", you can require taxId to be mandatory.

2. Composition Keywords

JSON Schema provides four composition keywords:

3. References ($ref)

The $ref keyword lets you reference other schema definitions for structure reuse. This is especially valuable in large APIs, preventing duplicate definitions of the same data structures.

Best Practice: Define commonly used data structures (addresses, pagination info) as standalone schemas, then reference them with $ref wherever needed — keeping things DRY (Don't Repeat Yourself).

JSON Schema and OpenAPI

The OpenAPI Specification (formerly Swagger) extensively uses JSON Schema to define API request and response formats. JSON Schema knowledge directly applies to OpenAPI documents.

OpenAPI 3.1 fully supports JSON Schema Draft 2020-12, enabling seamless integration between API documentation and data validation.

Popular Validation Libraries

LanguageLibraryNotes
JavaScriptAjvCurrently the fastest JSON Schema validator
PythonjsonschemaMost popular Python validation library
Javaeverit-org/json-schemaSupports Draft 4-7
Goxeipuuv/gojsonschemaGo JSON Schema validation
PHPjustinrainbow/json-schemaPHP JSON Schema validation

JSON Schema Version History

JSON Schema has evolved through multiple draft versions:

Start Validating Your JSON

Before writing schemas, make sure your JSON data is well-formed. Use our formatter tool for a quick check:

Try the JSON Formatter Tool →

Conclusion

JSON Schema is a powerful tool for ensuring data quality. Whether in API development, data pipelines, or form validation, it helps you define clear data contracts and reduce issues caused by malformed data. The time invested in learning JSON Schema will pay dividends in long-term project maintenance.

References

  1. JSON Schema Organization. "JSON Schema Specification." json-schema.org. https://json-schema.org/specification
  2. Wright, A. et al. "JSON Schema: A Media Type for Describing JSON Documents." IETF Internet-Draft, 2022. https://json-schema.org/draft/2020-12/json-schema-core
  3. OpenAPI Initiative. "OpenAPI Specification v3.1.0." OpenAPI, 2021. https://spec.openapis.org/oas/v3.1.0
  4. Ajv Contributors. "Ajv JSON schema validator." GitHub. https://ajv.js.org/