Type-specific keywords

The type keyword is fundamental to JSON Schema. It specifies the data type for a schema.

At its core, JSON Schema defines the following basic types:

These types have analogs in most programming languages, though they may go by different names.

Language-specific info:
Python
Ruby

The following table maps from the names of JSON types to their analogous types in Python:

JSONPython
stringstring *1
numberint/float *2
objectdict
arraylist
booleanbool
nullNone

Footnotes

[#1] Since JSON strings always support unicode, they are analogous to unicode on Python 2.x and str on Python 3.x.

[#2] JSON does not have separate types for integer and floating-point.

The type keyword may either be a string or an array:

  • If it's a string, it is the name of one of the basic types above.
  • If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, the JSON snippet is valid if it matches any of the given types.

Here is a simple example of using the type keyword:

schema
1
{ "type": "number" }
data
1
42
compliant to schema
data
1
42.0
compliant to schema
data
1
"42"
not compliant to schema

In the following example, we accept strings and numbers, but not structured data types:

schema
1
{ "type": ["number", "string"] }
data
1
42
compliant to schema
data
1
"Life, the universe, and everything"
compliant to schema
data
1
["Life", "the universe", "and everything"]
not compliant to schema

For each of these types, there are keywords that only apply to those types. For example, numeric types have a way of specifying a numeric range, that would not be applicable to other types. In this reference, these validation keywords are described along with each of their corresponding types in the following chapters.