const : Any

const

Any

Validation succeeds if the instance is equal to this keyword’s value.

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

The const keyword restricts an instance to a specific value. Its usage is functionally similar to an enum with a single value. Instances validate successfully only if their property value deeply matches the specified constant.

  • Applies to various JSON data types, including numbers, strings, booleans, objects, and arrays.
  • Takes precedence over other validation keywords like type and enum.

Examples

Schema with a specific string value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": "hello"
}
Valid An instance matching the const value is valid Instance
"hello"
Invalid An instance not matching the const value is invalid. Instance
"world"
Schema with a specific number value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": 3.14159
}
Valid An instance matching the const value is valid Instance
3.14159
Invalid An instance not matching the const value is invalid. Instance
"pi"
Schema with a fixed object structure Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": { "name": "John Doe", "age": 30 }
}
Invalid An empty object is invalid Instance
{}
Valid An instance matching the exact object structure is valid Instance
 { "name": "John Doe", "age": 30 }
Invalid An instance not matching the exact object structure is invalid Instance
 { "name": "Robert", "age": 30 }