examples : Array<Any>

examples

Array<Any>

This keyword is used to provide sample JSON values associated with a particular schema, for the purpose of illustrating usage.

Value This keyword must be set to an array of JSON values that preferrably successfully validates against the corresponding subschema
Kind Annotation
Applies To Any
Dialect 2020-12
Changed In None
Introduced In Draft 6
Vocabulary Meta Data
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-9.5
Metaschema https://json-schema.org/draft/2020-12/meta/meta-data
Official Tests draft2020-12/optional/refOfUnknownKeyword.json
Default []
Annotation Array The set of examples set by this keyword
Affected By None
Affects None
Also See

The examples keyword is used to provide a list of example instances associated with a particular schema that should ideally validate against the schema. These examples serve to illustrate the intended structure and constraints defined by the schema. While these examples are not used for validation purposes, they are helpful in providing sample valid instances against the schema they are defined in.

Note: While it is recommended that the examples validate against the subschema they are defined in, this requirement is not strictly enforced.

  • Used to demonstrate how data should conform to the schema.
  • examples does not affect data validation but serves as an informative annotation.

Examples

Schema with 'examples' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "examples": [ "foo", "bar", "Doe" ],
  "type": "string"
}
Valid An instance with a string value is valid Instance
"John"
Annotations
{ "keyword": "/examples", "instance": "", "value": [ "foo", "bar", "Doe" ] }
Schema with logical operators Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "if": {
    "properties": {
      "foo": { "const": "foo" }
    }
  },
  "then": {
    "properties": {
      "bar": {
        "type": "array",
        "examples": [ [ "foo" ], [ "bar", "baz" ] ]
      }
    }
  },
  "else": {
    "properties": {
      "bar": {
        "type": "boolean",
        "examples": [ false, true ]
      }
    }
  }
}
Valid Instance
{ "foo": "foo", "bar": [ "bar" ] }
Annotations
{ "keyword": "/then/properties/bar/examples", "instance": "/bar", "value": [ [ "foo" ], [ "bar", "baz" ] ] }
Valid Instance
{ "foo": "not foo", "bar": true }
Annotations
{ "keyword": "/else/properties/bar/examples", "instance": "/bar", "value": [ false, true ] }
Schema with multiple annotations for the same instance Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "examples": [ "John", "Karl" ],
  "$ref": "#/$defs/name",
  "$defs": {
    "name": {
      "examples": [ "John", "Karl" ],
      "type": "string"
    }
  }
}
Valid Instance
"John Doe"
Annotations
{ "keyword": "/examples", "instance": "", "value": [ "John", "Karl" ] }
{ "keyword": "/$ref/examples", "instance": "", "value": [ "John", "Karl" ] }