Sub topic 7
Naming entities with @id
This chapter assumes you can write a JSON-LD object with @context and @type, and that you know how to nest one entity inside another. If nesting is new to you, review the previous chapter first.
Why entities need names
Nesting describes a related thing inside the thing that uses it. That works until the same entity appears in several places. An organization might publish an article, own a website, and sell a product. Nesting its full description each time repeats data, and the copies can drift apart.
The @id keyword gives a node (one described entity) an identifier. Other nodes can then point to that identifier instead of repeating the description.
What an @id looks like
The value of @id is an IRI, which is a URL-like string that names the entity. It identifies the thing. It does not have to return a page.
{
"@context": "https://schema.org",
"@type": "Organization",
"@id": "https://example.com/#organization",
"name": "Example Coffee Co.",
"url": "https://example.com/"
}The #organization part is a fragment. A common convention is to take the page URL and add a fragment that says which entity on the page you mean. This keeps the identifier different from the page URL itself, because the page and the organization are different things.
Referencing a node
To point at a named entity, write an object that contains only @id.
{
"@context": "https://schema.org",
"@type": "Article",
"@id": "https://example.com/cold-coffee#article",
"headline": "How to Brew Cold Coffee",
"publisher": { "@id": "https://example.com/#organization" }
}The publisher value is a reference. A consumer that has also read the full Organization node can join the two by matching the identifier. Both nodes must be visible to the consumer. The next chapter shows how @graph places them in one block.
Rules of thumb
- Use the same string every time. Matching is exact, so a difference in scheme, host, path, or fragment creates a different entity.
- Use absolute URLs. A relative
@idis resolved against the page's base URL, which can change between pages and give the same entity different identifiers. - Keep identifiers stable. Do not put a date, a version, or a database row number that can be reused into the identifier.
- Give each entity one full description. Everywhere else, use the reference.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Reference to an `@id` that is never defined | The reference points to nothing, so the consumer learns only the identifier | Define the node in the same block, or make sure the identifier is meant to be external |
| Same entity written with two spellings of the `@id` | Treated as two separate entities | Build identifiers in one place in your code |
| Page URL used as the `@id` of a non-page entity | The organization and the page look like the same thing | Add a fragment such as `#organization` |
| Reference object with extra properties and a different `@type` | Conflicting descriptions of the same node | Put the full description in one place |
Exercise
A product page defines an Organization with "@id": "https://example.com/#organization". Its Product node uses "brand": { "@id": "https://example.com/#Organization" }. What goes wrong, and how do you prevent it?
Show answer
The fragment differs in letter case ("#Organization" versus "#organization"). Identifiers are matched exactly, so the brand reference does not connect to the Organization node. Prevent it by generating every identifier from one function or constant in your code, and use the same string in both places.