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.

Kind Assertion
Applies To Array
Dialect 2020-12
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
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.

  • The value of this keyword must be a boolean.
  • This keyword, when set to true, specifies that all elements in an array must be unique.
  • If it is set to false, the array can contain duplicate items.
  • Omitting this keyword has the same behavior as a value of false.

Examples

Schema with 'uniqueItems' property set to true Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "uniqueItems": true
}
An array instance with unique elements is valid Instance
[ 1, "hello", true ]
An instance with duplicate elements is invalid Instance
[ false, "world", 2, 2 ]
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
}
An array instance with unique elements is valid Instance
[ 1, "hello", true ]
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
}
An array instance with unique objects is valid Instance
[
  { "id": 1, "name": "John" },
  { "id": 2, "name": "Doe" }
]
An instance with duplicate objects is invalid Instance
[
  { "id": 1, "name": "Jane" },
  { "id": 1, "name": "Jane" }
]