uniqueItems : Boolean

uniqueItems

Boolean

If this keyword is set to the boolean value true, the instance validates successfully if all of its elements are unique.

Value This keyword must be set to a boolean value
Kind Assertion
Applies To Array
Dialect 2020-12
Changed In None
Introduced In Draft 2
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.4.3
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Official Tests draft2020-12/uniqueItems.json
Default false
Annotation None
Affected By None
Affects None
Also See

The uniqueItems keyword is used to ensure that all the items in an array are unique. This keyword is particularly useful when you need to enforce that an array contains no duplicate elements.

  • This keyword, when set to true, specifies that all elements in an array must be unique.

Examples

Schema with 'uniqueItems' property set to true Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "uniqueItems": true
}
Valid An array instance with unique elements is valid Instance
[ 1, "hello", true ]
Invalid An instance with duplicate elements is invalid Instance
[ false, "world", 2, 2 ]
Invalid An instance with duplicate complex structures (objects) is invalid Instance
[ { "name": "John" }, false, "world", 2, { "name": "John" } ]
  • Element uniqueness also deeply applies for complex structures like objects.
Schema without the 'uniqueItems' property or with 'uniqueItems' property set to false Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array"
}
  // or
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "uniqueItems": false
}
Valid An array instance with unique elements is valid Instance
[ 1, "hello", true ]
Valid An array instance with duplicate elements is also valid Instance
[ false, "world", 2, 2 ]
  • uniqueItems can be used with other array keywords like items and prefixItems to add more constraints to the instance. See the example below.
Schema with 'uniqueItems' and 'items' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "name": { "type": "string" }
    },
    "required": [ "id", "name" ]
  },
  "uniqueItems": true
}
Valid An array instance with unique objects is valid Instance
[
  { "id": 1, "name": "John" },
  { "id": 2, "name": "Doe" }
]
Invalid An instance with duplicate objects is invalid Instance
[
  { "id": 1, "name": "Jane" },
  { "id": 1, "name": "Jane" }
]