Sub topic 2
Describing Related Things with JSON-LD
Assume you know basic HTML, JSON objects and arrays, URLs, and how web pages are published. This chapter focuses on modeling several related entities in JSON-LD.
Nesting related entities
A JSON-LD object can contain another JSON-LD object as a property value. This is called nesting. Nesting is useful when one entity naturally belongs to another, such as an Article having an author.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "How to Brew Cold Coffee",
"author": {
"@type": "Person",
"name": "Ana Torres",
"url": "https://example.com/authors/ana"
}
}
</script>The nested object describes a Person. Its properties are interpreted using the schema.org vocabulary because the outer object declares @context.
Naming entities with `@id`
Use @id to give an entity a stable identifier, usually an absolute URL. Other objects can refer to that identifier instead of repeating the entity's details.
{
"@type": "Article",
"@id": "https://example.com/cold-coffee#article",
"headline": "How to Brew Cold Coffee",
"author": {
"@id": "https://example.com/authors/ana"
}
}An @id identifies the entity. It does not automatically provide its name or other properties.
Connecting entities with `@graph`
@graph groups several related nodes in one JSON-LD document. This works well when an organization, website, and article are reused across pages.
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "Organization",
"@id": "https://example.com/#organization",
"name": "Example Coffee Co."
},
{
"@type": "Article",
"@id": "https://example.com/cold-coffee#article",
"headline": "How to Brew Cold Coffee",
"publisher": {
"@id": "https://example.com/#organization"
}
}
]
}Embedding JSON-LD in HTML
Place JSON-LD in a script element with type="application/ld+json". The browser does not display this content as page text. Search engines and other tools can read it.
Common types
| Type | Common purpose |
|---|---|
| `Article` | Describe an article, headline, author, and dates |
| `Product` | Describe an item and its `Offer` |
| `BreadcrumbList` | Describe a page's navigation path |
An Organization commonly identifies the publisher. A Product can nest an Offer with price, priceCurrency, and availability. A BreadcrumbList contains ListItem objects with position and name.
Exercise
When should you use @id and @graph instead of repeating an entity's properties?
Show answer
Use @id to identify and reference one entity, and use @graph to group multiple related entities in one document.