Reference

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
Perl
Objective-C
Swift

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
{ "type": "number" }
data
42
compliant to schema
data
42.0
compliant to schema
data
"42"
not compliant to schema

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

schema
{ "type": ["number", "string"] }
data
42
compliant to schema
data
"Life, the universe, and everything"
compliant to schema
data
["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.

Format

The format keyword allows for basic semantic identification for non string types values also that are commonly used. For example, because JSON doesn't have a "DateTime" type, dates need to be encoded as strings. format allows the schema author to indicate that the string value should be interpreted as a date. By default, format is just an annotation and does not effect validation.

Optionally, validator implementations can provide a configuration option to enable format to function as an assertion rather than just an annotation. That means that validation will fail if, for example, a value with a date format isn't in a form that can be parsed as a date. This can allow values to be constrained beyond what the other tools in JSON Schema, including Regular Expressions can do.

Implementations may provide validation for only a subset of the built-in formats or do partial validation for a given format. For example, some implementations may consider a string an email if it contains a @, while others might do additional checks for other aspects of a well formed email address.

There is a bias toward networking-related formats in the JSON Schema specification, most likely due to its heritage in web technologies. However, custom formats may also be used, as long as the parties exchanging the JSON documents also exchange information about the custom format types. A JSON Schema validator will ignore any format type that it does not understand.

Built-in formats

The following is the list of formats specified in the JSON Schema specification.

Dates and times

Dates and times are represented in RFC 3339, section 5.6. This is a subset of the date format also commonly known as ISO8601 format.

  • "date-time": Date and time together, for example, 2018-11-13T20:20:39+00:00.
  • "time":
    New in draft 7
    Time, for example, 20:20:39+00:00
  • "date":
    New in draft 7
    Date, for example, 2018-11-13.
  • "duration":
    New in draft 2019-09
    A duration as defined by the ISO 8601 ABNF for "duration".
schema
{ "type": "string", "format": "date-time", }
For example, `PT1` expresses a duration of 1 minute.
data
{ "event": "Conference Call", "scheduledTime": "2024-09-24T10:00:00Z", "duration": "PT1H30M", "unixTimestamp": 1695553200 }
This example represents an event with start and end times, including both ISO 8601 date-time formats and a Unix timestamp.

Email addresses

  • "email": Internet email address, see RFC 5321, section 4.1.2.
  • "idn-email":
    New in draft 7
    The internationalized form of an Internet email address, see RFC 6531.
schema
{ "type": "string", "format": "email" }

For example, represents a user with a valid email.

data
{ "username": "johndoe", "email": "johndoe@example.com" }

Hostnames

schema
{ "type": "string", "format": "hostname" }

For example, represents a configuration object with a valid hostname.

data
{ "service": "webserver", "hostname": "example.com" }

IP Addresses

schema
{ "type": "string", "format": "ipv4" or "ipv6" }

For example, represents a configuration object that includes both IPv4 and IPv6 addresses.

data
{ "service": "database", "ipv4": "192.168.1.1", "ipv6": "2001:0db8:85a3:0000:0000:8a2e:0370:7334" }

Resource identifiers

  • "uuid":
    New in draft 2019-09
    A Universally Unique Identifier as defined by RFC 4122. Example: 3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a
  • "uri": A universal resource identifier (URI), according to RFC3986.
  • "uri-reference":
    New in draft 6
    A URI Reference (either a URI or a relative-reference), according to RFC3986, section 4.1.
  • "iri":
    New in draft 7
    The internationalized equivalent of a "uri", according to RFC3987.
  • "iri-reference":
    New in draft 7
    The internationalized equivalent of a "uri-reference", according to RFC3987
schema
{ "type": "string", "format": "uuid" or "uri" or "iri" }

For example, represents a configuration object that includes a UUID and a URI.

data

{ "service": "resource-manager", "uuid": "3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a", "uri": "https://api.example.com/resource" }

If the values in the schema have the ability to be relative to a particular source path (such as a link from a webpage), it is generally better practice to use "uri-reference" (or "iri-reference") rather than "uri" (or "iri"). "uri" should only be used when the path must be absolute.

URI template

  • "uri-template":
    New in draft 6
    A URI Template (of any level) according to RFC6570. If you don't already know what a URI Template is, you probably don't need this value.
schema
{ "type": "string", "format": "uri-template" }

For Example,

data
"/users/{userId}/posts/{postId}"

JSON Pointer

  • "json-pointer":
    New in draft 6
    A JSON Pointer, according to RFC6901. There is more discussion on the use of JSON Pointer within JSON Schema in Structuring a complex schema. Note that this should be used only when the entire string contains only JSON Pointer content, e.g. /foo/bar. JSON Pointer URI fragments, e.g. #/foo/bar/ should use "uri-reference".
  • "relative-json-pointer":
    New in draft 7
    A relative JSON pointer.
schema
{ "type": "string", "format": "json-pointer" }

For Example,

data
"/foo/bar"

Regular Expressions

  • "regex":
    New in draft 7
    A regular expression, which should be valid according to the ECMA 262 dialect.

Be careful, in practice, JSON schema validators are only required to accept the safe subset of regular expressions described elsewhere in this document.

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