$id : URI Reference
$id
URI ReferenceThis keyword declares an identifier for the schema resource.
Value |
This keyword must be set to an absolute URI or a relative reference as defined by RFC 3986 without a fragment
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
---|---|
Kind | Identifier |
Applies To | Any |
Base Dialect | 2020-12 |
Changed In | 2019-09 |
Introduced In | Draft 6 |
Vocabulary | Core |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.2.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/core |
Official Tests | draft2020-12/optional/id.json |
Default | None |
Annotation | None |
Affected By | None |
Affects |
|
Also See |
|
The $id
keyword explicitly turns a schema into a schema resource (a schema
that is associated with a URI). Relative URIs are resolved against the
current base URI, which is either the closest parent $id
keyword
(applicable in the case of compound schemas), or the base URI as determined by
the context on which the schema is declared (i.e. serving a schema over HTTP
may implicitly award it such URL as the base).
Note that you cannot set this keyword to a URI that contains a fragment
identifier. Instead, fragment identifiers must be set with the $anchor
keyword.
Digging Deeper
This keyword directly applies (without modifications or extensions) the concept of URIs to schemas. If you are having a hard time following the concepts discussed in this page, it is probably because you don’t have a strong grasp of URIs yet (a notably hard but universal pre-requisite!).
To learn more about URIs, we strongly suggest studying the IETF RFC 3986 URI standard. To avoid confusion, note that there is also a WHATWG URL Standard that targets URLs in the context of web browsers. However, JSON Schema only implements and expects the IETF original standard.
Common Pitfall
In JSON Schema, schema identifiers are merely identifiers and no behaviour is imposed on them. In particular, JSON Schema does not guarantee that a schema with an HTTP URL identifier is actually resolvable at such URL. To avoid surprises, JSON Schema implementations must be careful with automatically sending remote network requests when encountering supposely resolvable schema identifiers.
Best Practice
It is strongly recommended for every schema file to explicitly declare an absolute URI using this keyword. By doing so, you completely avoid various complex URI resolution edge cases, mainly when the base URI is implicit and context-dependent.
If you are serving schemas over the network (i.e. over HTTP), it is common to set this keyword to the target URL. However, if your schemas are not accessible over the network, prefer using a URN (with a valid namespace registered by IANA) or a non-locatable URI scheme such as a Tag URI.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/even-number.json",
"type": "number",
"multipleOf": 2
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "tag:example.com,2025:even-number",
"type": "number",
"multipleOf": 2
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "urn:example:even-number",
"type": "number",
"multipleOf": 2
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$comment": "This is the root schema resource",
"$id": "https://example.com/schemas/root.json",
"properties": {
"foo": {
"$comment": "The resolved URI of this nested schema resource is https://example.com/schemas/foo.json",
"$id": "foo.json",
},
"bar": {
"$comment": "The resolved URI of this nested schema resource is https://example.com/schemas/bar.json",
"$id": "/schemas/bar.json"
},
"baz": {
"$comment": "The resolved URI of this nested schema resource is https://absolute.example/baz.json",
"$id": "https://absolute.example/baz.json",
"items": {
"$comment": "The resolved URI of this nested schema resource is https://absolute.example/deep",
"$id": "deep"
}
}
}
}