Sub topic 4
Declaring @context and @type
This chapter assumes you can read JSON and know the basic JSON-LD shape: an object holding key-value pairs, embedded in a script element. It covers the two keywords every schema.org block needs. @context tells a parser what your words mean. @type says what kind of thing the object describes.
What @context does
JSON keys are plain strings. A parser reading "headline" has no way to know whether you mean the schema.org property, a column name in your database, or something else. The @context keyword fixes this. It maps short terms to full identifiers, which are IRIs (Internationalized Resource Identifiers, the generalization of URLs).
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "How to Brew Cold Coffee"
}A JSON-LD processor reads this and rewrites each term as a full IRI. This step is called expansion.
| Term in your markup | Expanded IRI |
|---|---|
| `Article` | `https://schema.org/Article` |
| `headline` | `https://schema.org/headline` |
Your page never needs to contain the long forms. The context is the lookup table that produces them.
Forms the value can take
@context accepts three forms.
- A string. A URL pointing to a context document.
"https://schema.org"is the form you will use almost always. - An object. The term definitions are written inline.
- An array. It combines the first two, so you can load schema.org and add your own terms.
The inline object form is useful for seeing what the schema.org context does. It sets a default vocabulary:
{
"@context": { "@vocab": "https://schema.org/" },
"@type": "Article",
"headline": "How to Brew Cold Coffee"
}@vocab means "any term with no other definition expands by appending it to this IRI." The published schema.org context behaves much like this, which has a consequence covered under the pitfalls below.
The array form lets you mix in a custom prefix:
{
"@context": [
"https://schema.org",
{ "ex": "https://example.com/terms#" }
],
"@type": "Article",
"headline": "How to Brew Cold Coffee",
"ex:roastLevel": "medium"
}Here ex:roastLevel is a compact IRI. The prefix ex is replaced with its definition, giving https://example.com/terms#roastLevel. Search engines will ignore it, but the document stays valid JSON-LD.
Where the context goes
Put @context once, on the outermost object. Nested objects inherit it, so do not repeat it on each child. With a @graph (a later chapter), the single context sits beside the @graph array and covers every node in it.
https or http
Both https://schema.org and http://schema.org are accepted by major consumers. Use https://schema.org and use it consistently. Mixed schemes across a site make auditing harder, and some tools compare the strings literally.
What @type does
@type names the class of the thing the object describes. In schema.org, a class is a category such as Article, Person, or Product. The value is a term resolved through the context, so "Article" becomes https://schema.org/Article.
{
"@context": "https://schema.org",
"@type": "Person",
"name": "Ana Torres"
}Each object that represents a distinct entity should have its own @type. A nested author gets Person, and a nested price object gets Offer:
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "How to Brew Cold Coffee",
"author": {
"@type": "Person",
"name": "Ana Torres"
}
}Choosing the right type
schema.org types form a hierarchy. Restaurant is a kind of FoodEstablishment, which is a kind of LocalBusiness, which is a kind of Organization. The properties of each parent are inherited by its children. Use the most specific type that is accurate. A property valid on LocalBusiness is also valid on Restaurant, and the specific type tells consumers more.
@type values are case-sensitive and use PascalCase (capital first letter, no spaces). Properties use camelCase (lowercase first letter). The case of the first letter is how you tell types and properties apart at a glance.
More than one type
@type may be an array when one thing really is two kinds of thing:
{
"@context": "https://schema.org",
"@type": ["Book", "Product"],
"name": "The Cold Brew Handbook",
"offers": {
"@type": "Offer",
"price": "18.00",
"priceCurrency": "USD"
}
}The object gets the properties of both types. Use this only when both descriptions are true. Do not add types in the hope of matching more search features.
Objects without @type
@type is optional in JSON-LD. A nested object that fills a property whose expected type is unambiguous can omit it. But consumers like Google's Rich Results Test often need the type to know how to read the object, and an untyped object is more likely to be ignored. Always write @type on every entity.
Checking context and type with code
A small script can catch the most common mistakes before a page ships. This one parses a JSON-LD string and reports missing or suspicious values on the top-level object:
import json
def check_node(raw: str) -> list[str]:
problems = []
node = json.loads(raw)
context = node.get("@context")
if context is None:
problems.append("missing @context")
elif isinstance(context, str) and context.rstrip("/") not in (
"https://schema.org",
"http://schema.org",
):
problems.append(f"unexpected @context: {context}")
node_type = node.get("@type")
if node_type is None and "@graph" not in node:
problems.append("missing @type")
else:
types = node_type if isinstance(node_type, list) else [node_type]
for t in types:
if t is not None and not t[:1].isupper():
problems.append(f"type should start with a capital: {t}")
return problems
sample = '{"@context": "https://schema.org", "@type": "article", "headline": "Hi"}'
print(check_node(sample))This prints ['type should start with a capital: article']. It checks form only. It does not know the full list of schema.org types, so use a validator for that (covered in a later chapter).
Common errors
- Missing
@context. The block is still valid JSON, but a consumer has no vocabulary for it and will usually ignore it. - Misspelled type or property.
"@type": "Artcle"is not an error to a JSON-LD processor. Because the schema.org context sets@vocab, the processor expands the misspelling tohttps://schema.org/Artcleand moves on. Only a schema.org-aware validator will flag it. - Wrong case.
"article"and"Article"are different terms. Only the second is a schema.org type. - Putting a type in a property slot.
"author": "Person"sets the author to the text "Person". The type belongs on a nested object:"author": { "@type": "Person", ... }. - Repeating
@contextin nested objects. This is harmless when it matches, and a source of conflicts when it does not. State it once. - Using a context URL that you control but cannot always serve. A remote context is fetched over the network, and a failed fetch can break expansion in strict processors. Stick to the schema.org URL, which major consumers know in advance.
Exercises
Exercise
A teammate writes the block below. It parses as JSON without errors, but a search engine ignores it. Name two problems and fix them. { "@type": "article", "headline": "Cold Brew Basics", "author": "Person" }
Show answer
First, there is no "@context", so the properties have no vocabulary. Add "@context": "https://schema.org". Second, "article" should be "Article", because types are case-sensitive and PascalCase. A third problem is that "author": "Person" sets the author to the text "Person", so it should be a nested object: "author": { "@type": "Person", "name": "Ana Torres" }.
Exercise
What does this compact IRI expand to, and why would a search engine not use it for rich results? { "@context": ["https://schema.org", { "ex": "https://example.com/terms#" }], "@type": "Article", "ex:roastLevel": "medium" }
Show answer
"ex:roastLevel" expands to https://example.com/terms#roastLevel. It is a custom term outside schema.org, so search engines have no defined meaning for it and ignore it. The document is still valid JSON-LD.