Can a list be JSON?

Arrays are used for ordered elements. In JSON, each element in an array may be of a different type.

In Python, "array" is analogous to a list or tuple type, depending on usage. However, the json module in the Python standard library will always use Python lists to represent JSON arrays.

In Ruby, "array" is analogous to a Array type.

[3, "different", { "types" : "of values" }]

There are two ways in which arrays are generally used in JSON:

  • List validation: a sequence of arbitrary length where each item matches the same schema.
  • Tuple validation: a sequence of fixed length where each item may have a different schema. In this usage, the index (or location) of each item is meaningful as to how the value is interpreted. (This usage is often given a whole separate type in some programming languages, such as Pythons tuple).

List validation is useful for arrays of arbitrary length where each item matches the same schema. For this kind of array, set the items keyword to a single schema that will be used to validate all of the items in the array.

In the following example, we define that each item in an array is a number:

{ "type": "array", "items": { "type": "number" } }

Tuple validation is useful when the array is a collection of items where each has a different schema and the ordinal index of each item is meaningful.

For example, you may represent a street address such as:

1600 Pennsylvania Avenue NW

as a 4-tuple of the form:

[number, street_name, street_type, direction]

Each of these fields will have a different schema:

  • number: The address number. Must be a number.
  • street_name: The name of the street. Must be a string.
  • street_type: The type of street. Should be a string from a fixed set of values.
  • direction: The city quadrant of the address. Should be a string from a different set of values.

To do this, we use the prefixItems keyword. prefixItems is an array, where each item is a schema that corresponds to each index of the documents array. That is, an array where the first element validates the first element of the input array, the second element validates the second element of the input array, etc.

In Draft 4 - 2019-09, tuple validation was handled by an alternate form of the items keyword. When items was an array of schemas instead of a single schema, it behaved the way prefixItems behaves.

Heres the example schema:

{ "type": "array", "prefixItems": [ { "type": "number" }, { "type": "string" }, { "enum": ["Street", "Avenue", "Boulevard"] }, { "enum": ["NW", "NE", "SW", "SE"] } ] }

[1600, "Pennsylvania", "Avenue", "NW"]

[10, "Downing", "Street"]

[1600, "Pennsylvania", "Avenue", "NW", "Washington"]

The items keyword can be used to control whether its valid to have additional items in a tuple beyond what is defined in prefixItems. The value of the items keyword is a schema that all additional items must pass in order for the keyword to validate.

Before to Draft 2020-12, you would use the additionalItems keyword to constrain additional items on a tuple. It works the same as items, only the name has changed.

In Draft 6 - 2019-09, the additionalItems keyword is ignored if there is not a "tuple validation" items keyword present in the same schema.

Here, well reuse the example schema above, but set items to false, which has the effect of disallowing extra items in the tuple.

{ "type": "array", "prefixItems": [ { "type": "number" }, { "type": "string" }, { "enum": ["Street", "Avenue", "Boulevard"] }, { "enum": ["NW", "NE", "SW", "SE"] } ], "items": false }

[1600, "Pennsylvania", "Avenue", "NW"]

[1600, "Pennsylvania", "Avenue"]

[1600, "Pennsylvania", "Avenue", "NW", "Washington"]

You can express more complex constraints by using a non-boolean schema to constrain what value additional items can have. In that case, we could say that additional items are allowed, as long as they are all strings:

{ "type": "array", "prefixItems": [ { "type": "number" }, { "type": "string" }, { "enum": ["Street", "Avenue", "Boulevard"] }, { "enum": ["NW", "NE", "SW", "SE"] } ], "items": { "type": "string" } }

[1600, "Pennsylvania", "Avenue", "NW", "Washington"]

[1600, "Pennsylvania", "Avenue", "NW", 20500]

New in draft 2019-09

Documentation Coming Soon

New in draft 6

While the items schema must be valid for every item in the array, the contains schema only needs to validate against one or more items in the array.

{ "type": "array", "contains": { "type": "number" } }

["life", "universe", "everything", 42]

["life", "universe", "everything", "forty-two"]

New in draft 2019-09

minContains and maxContains can be used with contains to further specify how many times a schema matches a contains constraint. These keywords can be any non-negative number including zero.

{ "type": "array", "contains": { "type": "number" }, "minContains": 2, "maxContains": 3 }

["apple", "orange", 2, 4]

["apple", "orange", 2, 4, 8]

["apple", "orange", 2, 4, 8, 16]

The length of the array can be specified using the minItems and maxItems keywords. The value of each keyword must be a non-negative number. These keywords work whether doing list validation or Tuple validation.

{ "type": "array", "minItems": 2, "maxItems": 3 }

A schema can ensure that each of the items in an array is unique. Simply set the uniqueItems keyword to true.

{ "type": "array", "uniqueItems": true }