Arrays are used for ordered elements. In JSON, each element in an array may be of a different type.
Language-specific info : Objective-CIn Python, "array" is analogous to the 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.
[ 1 , 2 , 3 , 4 , 5 ] compliant to schema [ 3 , "different" , < " types " : "of values" >] compliant to schema not compliant to schemaThere are two ways in which arrays are generally used in JSON:
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:
[ 1 , 2 , 3 , 4 , 5 ] compliant to schemaA single "non-number" causes the whole array to be invalid:
[ 1 , 2 , "3" , 4 , 5 ] not compliant to schemaThe empty array is always valid:
compliant to schemaTuple 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 ][number, street_name, street_type, direction]
Each of these fields will have a different schema:
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 document's 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.
Draft-specific infoIn 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.
Here's the example schema: