Reference

The basics

In What is a schema?, we described what a schema is, and hopefully justified the need for schema languages. Here, we proceed to write a simple JSON Schema.

Hello, World!

When learning any new language, it's often helpful to start with the simplest thing possible. In JSON Schema, an empty object is a completely valid schema that will accept any valid JSON.

Copy icon
 logo-white schema
{}

This accepts anything, as long as it's valid JSON

Copy icon
data
42
Checkmark iconcompliant to schema
Copy icon
data
"I'm a string"
Checkmark iconcompliant to schema
Copy icon
data
{ "an": [ "arbitrarily", "nested" ], "data": "structure" }
Checkmark iconcompliant to schema
New in draft 6

You can also use true in place of the empty object to represent a schema that matches anything, or false for a schema that matches nothing.

This accepts anything, as long as it's valid JSON:

Copy icon
 logo-white schema
true
Copy icon
data
42
Checkmark iconcompliant to schema
Copy icon
data
"I'm a string"
Checkmark iconcompliant to schema
Copy icon
data
{ "an": [ "arbitrarily", "nested" ], "data": "structure" }
Checkmark iconcompliant to schema

Documents for this schema will always be invalid:

Copy icon
 logo-white schema
false
Copy icon
data
"Resistance is futile... This will always fail!!!"
Error iconnot compliant to schema

The type keyword

Of course, we wouldn't be using JSON Schema if we wanted to just accept any JSON document. The most common thing to do in a JSON Schema is to restrict to a specific type. The type keyword is used for that.

When this book refers to JSON Schema "keywords", it means the "key" part of the key/value pair in an object. Most of the work of writing a JSON Schema involves mapping a special "keyword" to a value within an object.

For example, in the following, only strings are accepted:

Copy icon
 logo-white schema
{ "type": "string" }
Copy icon
data
"I'm a string"
Checkmark iconcompliant to schema
Copy icon
data
42
Error iconnot compliant to schema

The type keyword is described in more detail in here.

Declaring a JSON Schema

It's not always easy to tell which draft a JSON Schema is using. You can use the $schema keyword to declare which version of the JSON Schema specification the schema is written to. See $schema for more information. It's generally good practice to include it, though it is not required.

For brevity, the $schema keyword isn't included in most of the examples in this book, but it should always be used in the real world.

Copy icon
 logo-white schema
{ "$schema": "https://json-schema.org/draft/2020-12/schema" }
Draft-specific info
In Draft 4, a $schema value of http://json-schema.org/schema# referred to the latest version of JSON Schema. This usage has since been deprecated and the use of specific version URIs is required.

Declaring a unique identifier

New in draft 6

It is also best practice to include an $id property as a unique identifier for each schema. For now, just set it to a URL at a domain you control, for example:

Copy icon
 logo-white schema
{ "$id": "http://yourdomain.com/schemas/myschema.json" }

The details of $id become more apparent when you start structuring a complex schema.

Draft-specific info
In Draft 4, $id is just id (without the dollar-sign).

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!.