JSON Schema is a vocabulary for annotating and validating JSON documents. It lets you define the expected structure of a JSON object: which fields are required, what types the values must be, what patterns strings must match, and what constraints numbers must satisfy. A JSON Schema is itself written in JSON and can be used to validate any JSON document against those rules.
Why JSON Schema Matters
APIs are more reliable when they validate their inputs and outputs against a schema. Instead of discovering at runtime that a required field is missing or a value has the wrong type, schema validation catches these problems immediately and returns a clear error message.
Documentation generated from a JSON Schema is automatically accurate because the schema is the authoritative description of the data structure.
Testing becomes more thorough when test data is validated against the schema before being used in test cases.
Contract testing between services is made formal when both sides agree on a shared schema. Changes to the API that violate the schema are caught in CI before they reach production.
JSON Schema Basics
A JSON Schema document uses keywords to describe the expected data. The type keyword specifies the JSON type (string, number, integer, boolean, array, or object). The properties keyword defines the expected properties of an object. The required keyword lists which properties must be present. The items keyword defines the expected type of array elements. The minimum and maximum keywords constrain numeric values. The pattern keyword validates strings against a regular expression.
A schema can reference other schemas using the $ref keyword, enabling modular schema design where common types are defined once and reused.
Generating a Schema from Sample Data
The most practical way to create a JSON Schema is to start with an example of the data you want to validate and let a tool infer the schema. The generated schema reflects the types and structure of the sample. You then add constraints and mark required fields based on your actual business rules.
Using the DevHexLab JSON Schema Generator
Open the tool at /tools/developer/json-schema-generator. Paste a representative JSON sample. The tool generates a draft JSON Schema with the inferred types and structure. Edit the schema in the output panel to add required fields, patterns, and constraints. Copy the schema and use it with any JSON Schema validator.
Frequently Asked Questions
Which JSON Schema version should I use?
Draft 2020-12 is the current specification. Draft-07 is the most widely supported by existing libraries. Check which version your validation library supports before choosing.
Can JSON Schema validate nested objects?
Yes. Nested objects are described with nested schema objects under the properties keyword. Arrays of objects use the items keyword with a nested object schema.
Generate the schema, add your constraints, and validate at every boundary.