JSON is flexible, but applications often need data to follow a predictable structure. JSON Schema provides a way to describe the expected shape of a JSON document, including property names, data types, required fields and validation rules. It helps teams define a contract between services and catch unexpected input early.
When reviewing sample payloads against a schema, the GetConvertor JSON Prettifier can make the JSON easier to read before you run it through a schema validator.
What is JSON Schema?
JSON Schema is a vocabulary for describing and validating the structure of JSON data. A schema can state that a field must be a string, that a number must fall within a range, or that an object must contain specific properties. The schema is itself written in JSON.
A simple JSON Schema example
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"],
"additionalProperties": false
}
This example describes an object that must contain a string property called name. It may also contain a non-negative integer age, and it disallows undeclared properties.
Common JSON Schema keywords
- type: Defines the expected type, such as object, array, string, number, integer, boolean or null.
- properties: Describes the fields available on an object.
- required: Lists properties that must be present.
- items: Defines the schema for elements in an array.
- minimum and maximum: Set numeric boundaries.
- minLength and maxLength: Restrict string length.
- enum: Limits a value to a defined set of options.
- additionalProperties: Controls whether undeclared object fields are allowed.
Why use JSON Schema?
Schemas make API contracts easier to communicate, improve input validation, support form generation and reduce ambiguity between frontend and backend teams. They are especially helpful when multiple services exchange data or when a payload evolves over time.
JSON syntax validation vs. schema validation
Syntax validation asks whether the text is valid JSON. Schema validation asks whether valid JSON follows a particular data contract. A document can parse successfully but still fail its schema because a required property is missing or a value has the wrong type.
Best practices
- Keep schemas close to the API or data contract they describe.
- Use clear property names and document important constraints.
- Decide deliberately whether additional properties are allowed.
- Version schemas when changes may break existing consumers.
- Test both valid and invalid examples.
Format schema files for easier review
Schema files are JSON too, so consistent indentation helps reviewers understand nested rules. Use the GetConvertor JSON Prettifier to pretty print a schema or example payload, then use a dedicated JSON Schema validator to check the document against the schema.
Frequently asked questions
Is JSON Schema the same as JSON?
No. JSON is the data format; JSON Schema is a way to describe and validate the structure of JSON documents.
Does a JSON parser validate a schema?
A regular JSON parser checks syntax. Schema validation requires a JSON Schema validator and the schema rules.
Can JSON Schema make a field mandatory?
Yes. Add the property’s name to the required array of its containing object schema.