Miscellaneous Examples

Basic

This example provides a typical minimum you are likely to see in JSON Schema. It contains:

  • $id keyword
  • $schema keyword
  • title annotation keyword
  • type instance data model
  • properties validation keyword
  • Three keys: firstName, lastName and age each with their own:
    • description annotation keyword.
    • type instance data model (see above).
  • minimum validation keyword on the age key.
schema
1
{
2
"$id": "https://example.com/person.schema.json",
3
"$schema": "https://json-schema.org/draft/2020-12/schema",
4
"title": "Person",
5
"type": "object",
6
"properties": {
7
"firstName": {
8
"type": "string",
9
"description": "The person's first name."
10
},
11
"lastName": {
12
"type": "string",
13
"description": "The person's last name."
14
},
15
"age": {
16
"description": "Age in years which must be equal to or greater than zero.",
17
"type": "integer",
18
"minimum": 0
19
}
20
}
21
}

Data

data
1
{
2
"firstName": "John",
3
"lastName": "Doe",
4
"age": 21
5
}

Describing geographical coordinates.

This example introduces:

schema
1
{
2
"$id": "https://example.com/geographical-location.schema.json",
3
"$schema": "https://json-schema.org/draft/2020-12/schema",
4
"title": "Longitude and Latitude Values",
5
"description": "A geographical coordinate.",
6
"required": [ "latitude", "longitude" ],
7
"type": "object",
8
"properties": {
9
"latitude": {
10
"type": "number",
11
"minimum": -90,
12
"maximum": 90
13
},
14
"longitude": {
15
"type": "number",
16
"minimum": -180,
17
"maximum": 180
18
}
19
}
20
}

Data

data
1
{
2
"latitude": 48.858093,
3
"longitude": 2.294694
4
}

Arrays of things

Arrays are fundamental structures in JSON -- here we demonstrate a couple of ways they can be described:

  • An array of string values.
  • An array of objects.

We also introduce the following with this example:

schema
1
{
2
"$id": "https://example.com/arrays.schema.json",
3
"$schema": "https://json-schema.org/draft/2020-12/schema",
4
"description": "A representation of a person, company, organization, or place",
5
"type": "object",
6
"properties": {
7
"fruits": {
8
"type": "array",
9
"items": {
10
"type": "string"
11
}
12
},
13
"vegetables": {
14
"type": "array",
15
"items": { "$ref": "#/$defs/veggie" }
16
}
17
},
18
"$defs": {
19
"veggie": {
20
"type": "object",
21
"required": [ "veggieName", "veggieLike" ],
22
"properties": {
23
"veggieName": {
24
"type": "string",
25
"description": "The name of the vegetable."
26
},
27
"veggieLike": {
28
"type": "boolean",
29
"description": "Do I like this vegetable?"
30
}
31
}
32
}
33
}
34
}

Data

data
1
{
2
"fruits": [ "apple", "orange", "pear" ],
3
"vegetables": [
4
{
5
"veggieName": "potato",
6
"veggieLike": true
7
},
8
{
9
"veggieName": "broccoli",
10
"veggieLike": false
11
}
12
]
13
}