Getting Started
Creating your first schema
JSON Schema is a vocabulary that you can use to annotate and validate JSON documents. This tutorial guides you through the process of creating a JSON Schema.
After creating your JSON Schema, you can then validate example data against your schema by using a validator in a language of your choice. Please, visit Tools and select the validator that better suit your needs.
If you already know how to create JSON Schemas and you are looking for different JSON Schema use cases like schema generation, code generation, documentation, UI generation or JSON Schema processing or conversion, please visit Tools and explore the amazing tooling available in the JSON Schema Ecosystem.
Overview
The example we use in this guide is a product catalog that stores its data using JSON objects, like the following:
Each product in the catalog has:
productId
: an identifier for the productproductName
: the product nameprice
: the cost to the consumertags
: an optional array of identifying tags
The JSON object is human-readable, but it doesn’t include any context or metadata. There’s no way to tell from looking at the object what the keys mean or what the possible inputs are. JSON Schema is a standard for providing answers to these questions. In this guide, you will create a JSON Schema document that describes the structure, constraints, and data types for a set of JSON data.
Introduction to JSON Schema
The instance is the JSON document that is being validated or described, and the schema is the document that contains the description.
The most basic schema is a blank JSON object, which constrains nothing, allows anything, and describes nothing:
By adding validation keywords to the schema, you can apply constraints to an instance. For example, you can use the type
keyword to constrain an instance to an object, array, string, number, boolean, or null:
JSON Schema is hypermedia-ready and ideal for annotating your existing JSON-based HTTP API. JSON Schema documents are identified by URIs, which can be used in HTTP link headers and within JSON Schema documents to allow for recursive definitions.
Create a schema definition
To create a basic schema definition, define the following keywords:
$schema
: specifies which draft of the JSON Schema standard the schema adheres to.$id
: sets a URI for the schema. You can use this unique URI to refer to elements of the schema from inside the same document or from external JSON documents.title
anddescription
: state the intent of the schema. These keywords don’t add any constraints to the data being validated.type
: defines the first constraint on the JSON data. In the product catalog example below, this keyword specifies that the data must be a JSON object.
For example:
The keywords are defined using JSON keys. Typically, the data being validated is contained in a JSON data document, but JSON Schema can also validate JSON data contained in other content types, such as text or XML files.
In JSON Schema terminology, $schema
and $id
are schema keywords, title
and description
are schema annotations, and type
is a validation keyword.
Define properties
This section adds the properties
keyword. In JSON Schema terms, properties
is a validation keyword. When you define properties
, you create an object where each property represents a key in the JSON data that’s being validated. You can also specify which properties defined in the object are required.
Add the properties object
Using the product catalog example, productId
is a numeric value that uniquely identifies a product. Since this is the canonical identifier for the product, it’s required.
To add the properties
object to the schema:
Add the
properties
validation keyword to the end of the schema:1 ... 2 "title": "Product", 3 "description": "A product from Acme's catalog", 4 "type": "object", 5 "properties": { 6 "productId": {} 7 }
Add the
productId
keyword, along with the following schema annotations:description
: describes whatproductId
is. In this case, it’s the product’s unique identifier.type
: defines what kind of data is expected. For this example, since the product identifier is a numeric value, useinteger
.1... 2 "properties": { 3 "productId": { 4 "description": "The unique identifier for a product", 5 "type": "integer" 6 } 7 }
With the new properties
validation keyword, the overall schema looks like this:
The following example adds another required key, productName
. This value is a string:
The properties
object now includes two keys, productId
and productName
. When JSON data is validated against this schema, validation fails for any documents that contain invalid data in either of these fields.
Define required properties
This section describes how to specify that certain properties are required. This example makes the two existing keys required and adds another required key named price
. The price
key has a description
and type
just like the other keys, but it also specifies a minimum value. Because nothing in the store is free, each product requires a price value that’s above zero. Define this using the exclusiveMinimum
validation keyword.
To define a required property:
Inside the
properties
object, add theprice
key. Include the usual schema annotationsdescription
andtype
, wheretype
is a number:1 "properties": { 2 ... 3 "price": { 4 "description": "The price of the product", 5 "type": "number" 6 } 7 }
Add the
exclusiveMinimum
validation keyword and set the value to zero:1 "price": { 2 "description": "The price of the product", 3 "type": "number", 4 "exclusiveMinimum": 0 5 }
Add the
required
validation keyword to the end of the schema, after theproperties
object. AddproductID
,productName
, and the newprice
key to the array:1 ... 2 "properties": { 3 ... 4 "price": { 5 "description": "The price of the product", 6 "type": "number", 7 "exclusiveMinimum": 0 8 }, 9 }, 10 "required": [ "productId", "productName", "price" ]
With the new required
keyword and price
key, the overall schema looks like this:
The exclusiveMinimum
validation keyword is set to zero, which means that only values above zero are considered valid. To include zero as a valid option, you could use the minimum
validation keyword instead.
Define optional properties
This section describes how to define an optional property. For this example, define a keyword named tags
using the following criteria:
- The
tags
keyword is optional. - If
tags
is included, it must contain at least one item. - All tags must be unique.
- All tags must be text.
To define an optional property:
Inside the
properties
object, add thetags
keyword. Include the usual schema annotationsdescription
andtype
, and definetype
as an array:1 ... 2 "properties": { 3 ... 4 "tags": { 5 "description": "Tags for the product", 6 "type": "array" 7 } 8 }
Add a new validation keyword for
items
to define what appears in the array. For example,string
:1 ... 2 "tags": { 3 "description": "Tags for the product", 4 "type": "array", 5 "items": { 6 "type": "string" 7 } 8 }
To make sure there is at least one item in the array, use the
minItems
validation keyword:1 ... 2 "tags": { 3 "description": "Tags for the product", 4 "type": "array", 5 "items": { 6 "type": "string" 7 }, 8 "minItems": 1 9 }
To make sure that every item in the array is unique, use the
uniqueItems
validation keyword and set it totrue
:1 ... 2 "tags": { 3 "description": "Tags for the product", 4 "type": "array", 5 "items": { 6 "type": "string" 7 }, 8 "minItems": 1, 9 "uniqueItems": true 10 }
With the new tags
keyword, the overall schema looks like this:
Because the new keyword is not required, there are no changes to the required
section.
Create a nested data structure
The earlier examples describe a flat schema with only one level. This section describes how to use nested data structures in JSON Schema.
To create a nested data structure:
Inside the
properties
object, create a new key calleddimensions
:1 ... 2 "properties": { 3 ... 4 "dimensions": {} 5 }
Define the
type
validation keyword asobject
:1 ... 2 "dimensions": { 3 "type": "object" 4 }
Add the
properties
validation keyword to contain the nested data structure. Inside the newproperties
keyword, add keywords forlength
,width
, andheight
that all use thenumber
type:1 ... 2 "dimensions": { 3 "type": "object", 4 "properties": { 5 "length": { 6 "type": "number" 7 }, 8 "width": { 9 "type": "number" 10 }, 11 "height": { 12 "type": "number" 13 } 14 } 15 }
To make each of these properties required, add a
required
validation keyword inside thedimensions
object:1 ... 2 "dimensions": { 3 "type": "object", 4 "properties": { 5 "length": { 6 "type": "number" 7 }, 8 "width": { 9 "type": "number" 10 }, 11 "height": { 12 "type": "number" 13 } 14 }, 15 "required": [ "length", "width", "height" ] 16 }
Using the new nested data structures, the overall schema looks like this:
The new required
validation keyword only applies within the scope of the dimensions
key.
Add an external reference
This section describes how to reference resources outside of the schema. Sharing schemas across many data structures is a common way to make them easier to use, read, and keep up-to-date. So far, the product catalog schema is self-contained. This section creates a new schema and then references it in the product catalog schema.
The following schema validates a geographical location:
To reference this schema in the product catalog schema:
Inside the
properties
object, add a key namedwarehouseLocation
:1 ... 2 "properties": { 3 ... 4 "warehouseLocation": {} 5 }
To link to the external geographical location schema, add the
$ref
schema keyword and the schema URL:1 ... 2 "warehouseLocation": { 3 "description": "Coordinates of the warehouse where the product is located.", 4 "$ref": "https://example.com/geographical-location.schema.json" 5 }
With the external schema reference, the overall schema looks like this:
Validate JSON data against the schema
Now that you have your JSON Schema is time to validate JSON data against it using a JSON Schema Validator.
A Validator is a tool that implements the JSON Schema specification. All validators works in a similar way: they take a JSON Schema and a JSON Instance as input and they returns the validation result as output.
To try it yourself, please visit Tools and select the validator that better suit your needs, our use the editors available below to explore the different Schemas and Instances and see the different validation results.
JSON Schema
1
JSON Instance
1
Validation Result
What Next?
Now that you know how to create a JSON Schema and use it to validate JSON data, we'd invite you to continue your JSON Schema journey:
- Learn more about JSON Schema by visiting the reference documentation.
- Explore the details of the current version of the Spec 2020-12.
If you already know how to create JSON Schemas and you are looking for different JSON Schema use cases like schema generation, code generation, documentation, UI generation or JSON Schema processing or conversion, please visit Tools and explore the amazing tooling available in the JSON Schema Ecosystem.
Need Help?
Did you find these docs helpful?
Help us make our docs great!
At JSON Schema, we value docs contributions as much as every other type of contribution!
Still Need Help?
Learning JSON Schema is often confusing, but don't worry, we are here to help!.