Reference
array
Arrays are used for ordered elements. In JSON, each element in an arraymay be of a different type.
In Python, "array" is analogous to thelist ortuple type,depending on usage. However, thejson module in the Pythonstandard library will always use Python lists to represent JSONarrays.
There are two ways in which arrays are generally used in JSON:
- List validation: a sequence of arbitrary length where each item matches the sameschema.
- 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 Python's
tuple).
Items
List validation is useful for arrays of arbitrary length where each itemmatches the same schema. For this kind of array, set theitemskeywordto a single schema that will be used to validate all of the items in thearray.
In the following example, we define that each item in an array is anumber:
A single "non-number" causes the whole array to be invalid:
The empty array is always valid:
Tuple validation
Tuple validation is useful when the array is a collection of items whereeach has a different schema and the ordinal index of each item ismeaningful.
For example, you may represent a street address such as1600 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 fixedset of values.direction: The city quadrant of the address. Should be a stringfrom a different set of values.
To do this, we use theprefixItems keyword.prefixItems is an array,where each item is a schema that corresponds to each index of thedocument's array. That is, an array where the first element validatesthe first element of the input array, the second element validates thesecond element of the input array, etc.
items keyword. Whenitems was an array of schemas instead of a single schema, it behaved the wayprefixItems behaves.Here's the example schema:
"Drive" is not one of the acceptable street types:
This address is missing a street number:
It's okay to not provide all of the items:
And, by default, it's also okay to add additional items to end:
Additional Items
Theitems keyword can be used to control whether it's valid to haveadditional items in a tuple beyond what is defined inprefixItems. Thevalue of theitems keyword is a schema that all additional items mustpass in order for the keyword to validate.
additionalItemskeyword to constrain additional items on a tuple. It works the sameasitems, only the name has changed.additionalItems keyword is ignored ifthere is not a "tuple validation"items keyword present in thesame schema.Here, we'll reuse the example schema above, but setitems tofalse,which has the effect of disallowing extra items in the tuple.
It's ok to not provide all of the items:
But, sinceitems isfalse, we can't provide extra items:
You can express more complex constraints by using a non-boolean schemato constrain what value additional items can have. In that case, wecould say that additional items are allowed, as long as they are allstrings:
Extra string items are ok ...
... but not anything else
Unevaluated Items
TheunevaluatedItems keyword is useful mainly when you want to addor disallow extra items to an array.
unevaluatedItems applies to any values not evaluated by anitems,prefixItems, orcontains keyword. Just asunevaluatedPropertiesaffects onlyproperties in an object,unevaluatedItems affectsonlyitems in an array.
Watch out! The word "unevaluated"does not mean "not evaluated byitems,prefixItems, orcontains." "Unevaluated" means"not successfully evaluated", or "does not evaluate to true".
Like withitems, if you setunevaluatedItems tofalse, youcan disallow extra items in the array.
Here, all the values are evaluated. The schema passes validation.
But here, the schema fails validation because"unevaluatedItems": falsespecifies that no extra values should exist.
Note thatitems doesn't "see inside" anyinstances ofallOf,anyOf, oroneOf in the samesubschema. So in this next example,items ignoresallOf and thus fails to validate.
But if you replaceitems withunevaluatedItems, then the samearray validates.
You can also make a "half-closed" schema: something useful when youwant to keep the first two arguments, but also add more in certainsituations. ("Closed" to two arguments in some places, "open" tomore arguments when you need it to be.)
"$defs":{"closed":{"$anchor":"closed","$ref":"#","unevaluatedItems":false}}}
Here the schema is "closed" to two array items. You can then lateruse$ref and add another item like this:
"$defs":{"closed":{"$anchor":"closed","$ref":"#","unevaluatedItems":false}}}
Thus, you would referencemy-tuple#closed when you need only twoitems and referencemy-tuple#extended when you need three items.
Contains
While theitems schema must be valid for every item in the array, thecontains schema only needs to validate against one or more items inthe array.
A single "number" is enough to make this pass:
But if we have no number, it fails:
All numbers is, of course, also okay:
minContains / maxContains
minContains andmaxContains can be used withcontains to furtherspecify how many times a schema matches acontains constraint. Thesekeywords can be any non-negative number including zero.
FailsminContains
FailsmaxContains
Length
The length of the array can be specified using theminItems andmaxItems keywords. The value of each keyword must be a non-negativenumber. These keywords work whether doinglist validation ortuple-validation.
Uniqueness
A schema can ensure that each of the items in an array is unique. Simplyset theuniqueItems keyword totrue.
The empty array always passes:
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!.