$ref : URI Reference

$ref

URI Reference

This keyword is used to reference a statically identified schema.

Value This keyword must be set to an absolute URI or a relative reference as defined by RFC 3986, where its fragment (if any) can consist of a JSON Pointer as defined by RFC 6901
Kind Applicator
Applies To Any
Dialect 2020-12
Changed In Draft 6 2019-09
Introduced In Draft 3
Vocabulary Core
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.2.3.1
Metaschema https://json-schema.org/draft/2020-12/meta/core
Official Tests
Default None
Annotation None
Affected By
Affects None
Also See

The $ref keyword is used to statically reference a schema. This is useful for avoiding code duplication and promoting modularity when describing complex data structures.

Examples

Schema with a relative reference Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/product.json",
  "type": "object",
  "properties": {
    "productId": { "type": "integer" },
    "name": { "$ref": "string" }
  },
  "required": [ "productId", "name" ],
  "$defs": {
    "string": {
      "$id": "string",
      "type": "string"
    }
  }
}
Valid An instance including all the required properties is valid Instance
{
  "productId": 123,
  "name": "Widget"
}
Invalid An object instance with name proeprty not set to string is invalid Instance
{
  "productId": 217,
  "name": 999
}
Schema with an absolute reference to the previous schema Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/order.json",
  "type": "object",
  "properties": {
    "items": {
      "type": "array",
      "items": { "$ref": "schemas/product.json" }
    }
  }
}
Valid Each item in the "items" array includes both the "productId" and "name" properties required by the referenced product schema Instance
{
  "items": [
    { "productId": 123, "name": "Widget" },
    { "productId": 456, "name": "Gadget" }
  ]
}
// Assuming http://example.com/schemas/product.json defines the product schema
Invalid The first item is missing the "productId" property and the second item is missing the "name" property required by the product schema. Instance
{
  "items": [
    { "name": "Widget" },
    { "productId": 456 }
  ]
}
Schema having an absolute reference with a JSON Pointer Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/product.json",
  "$ref": "https://example.com/schemas/product.json#/$defs/string",
  "$defs": {
    "string": { "type": "string" }
  }
}
Valid An instance with a string value is valid Instance
"John Doe"
Invalid An instance with a boolean value is invalid Instance
true
Schema having absolute reference with an anchor Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/product.json",
  "$ref": "https://example.com/schemas/product.json#string",
  "$defs": {
    "string": { "$anchor": "string", "type": "boolean" }
  }
}
Valid An instance with a boolean value is valid Instance
false
Invalid An instance with a numeric value is invalid Instance
99
Schema with a JSON Pointer Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com",
  "type": "object",
  "properties": {
    "name": { "$ref": "#/$defs/string" }
  },
  "required": [ "name" ],
  "$defs": {
    "string": { "type": "string" }
  }
}
Valid Instance including all the required properties is valid Instance
{
  "name": "John Doe"
}
Invalid Instance with name property set to boolean is invalid Instance
{
  "name": true
}
Schema with an anchor Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com",
  "type": "object",
  "properties": {
    "counter": { "$ref": "#counter" }
  },
  "required": [ "counter" ],
  "$defs": {
    "string": { "$anchor": "counter", "type": "number" }
  }
}
Valid Instance including all the required properties is valid Instance
{
  "counter": 51
}
Invalid Instance with counter property set to string is invalid Instance
{
  "counter": "59"
}
Schema with '$id' set to URN Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:example:vehicle",
  "$ref": "urn:example:phone",
  "$defs": {
    "phone": {
      "$id": "urn:example:phone",
      "type": "number"
    }
  }
}
Valid An instance with a numeric value is valid Instance
7843559621
Invalid An instance with a value other than a number is invalid Instance
{
  "phone": 9866548907
}