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.
The following table maps from the names of JSON types to their analogous types in Python:
JSON | Python |
string | string [4] |
number | int/float [5] |
object | dict |
array | list |
boolean | bool |
null | None |
Footnotes
[4] | Since JSON strings always support unicode, they are
analogous to unicode on Python 2.x and str on
Python 3.x. |
[5] | 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:
{ "type": "number" }
42
42.0
This is not a number, it is a string containing a number.
"42"
In the following example, we accept strings and numbers, but not structured data types:
{ "type": ["number", "string"] }
42
"Life, the universe, and everything"
["Life", "the universe", "and everything"]
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.