Reference
JSON data types
The type
keyword is fundamental to JSON Schema because it specifies the data type that a schema should expect.
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 [1] |
number | int/float [2] |
object | dict |
array | list |
boolean | bool |
null | None |
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 can take two forms:
- A single string. When it is a single string, it must be one of the types mentioned above (
array
,boolean
,integer
,number
,null
,object
,regular expressions
, orstring
). This specifies that the instance data is only valid when it matches that specific type.
Here is an example of using the string
keyword as a single string:
- An array of strings. When
type
is used as an array, it contains more than one string specifying the types mentioned above. In this case, the instance data is valid if it matches any of the given types.
Here is an example using the type
keyword as an array of strings, where instance data of the type string
and number
are valid but array
isn't:
JSON Schema offers a variety of keywords to validate data against specific types. The following table outlines keywords that are specifically designed for each basic data type:
Type Keyword | Specific Keywords | Description |
---|---|---|
array | items , additionalItems , minItems , maxItems , uniqueItems | Define item schemas, additional item handling, item count constraints, and uniqueness. |
number | minimum , maximum , exclusiveMinimum , exclusiveMaximum , multipleOf | Define numeric ranges, including exclusive bounds and divisibility. |
object | required , properties , additionalProperties , patternProperties , minProperties , maxProperties , dependencies | Define required properties, property schemas, additional property handling, pattern-based property matching, and property count constraints. |
string | minLength , maxLength , pattern , format | Restrict string length, pattern matching, and format validation (e.g., email, date). |
Understanding these basic data types gives you a strong foundation for building more complex JSON Schemas.
Dive deeper into our reference and explore JSON Schema's flexibility for creating complex data structures:
- Value restrictions. Define precise limitations for data, ensuring accuracy and consistency.
- Conditional schema validation. Validate schemas dynamically based on specific conditions.
- Schema composition. Build modular and reusable schemas, making your validation process more efficient and maintainable.
By utilizing these advanced features, you can create robust and flexible JSON Schemas that meet your exact needs.
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!.