Sub topic 2
JSON-LD as JSON with meaning
This chapter assumes you can read JSON (objects, arrays, strings, numbers, booleans) and know what a script element is in HTML.
What JSON-LD adds to JSON
JSON-LD stands for "JSON for Linked Data". It is a W3C standard, and every JSON-LD document is also valid JSON. Any JSON parser can read it, and you write it with the syntax you already know.
The difference is in how the content is interpreted. Plain JSON has no agreed meaning for its keys. In {"name": "Ana Torres"}, the key name could mean a person's name, a file name, or a product name. A program that reads this has to guess.
JSON-LD removes the guess. It reserves keys that begin with @, called keywords, and these tell a processor how to read the rest of the document.
{
"@context": "https://schema.org",
"@type": "Person",
"name": "Ana Torres",
"jobTitle": "Barista"
}Here @context says which vocabulary gives the keys their meaning. @type says what kind of thing the object describes. The remaining keys are properties of that thing.
How a processor reads the meaning
A JSON-LD processor uses the context to expand each short key into a full URL. With the context above, name becomes https://schema.org/name and jobTitle becomes https://schema.org/jobTitle. Two sites that both write name under the schema.org context therefore mean the same property. A site that used a different vocabulary would expand name to a different URL.
You almost never write the expanded form by hand. You write short keys and let the processor expand them. The later chapter on @context and @type covers the details.
The keywords you will meet
| Keyword | Purpose |
|---|---|
| `@context` | Names the vocabulary that gives keys their meaning |
| `@type` | States what kind of thing an object describes |
| `@id` | Gives a thing a unique identifier |
| `@graph` | Holds several things in one document |
This chapter uses only the first two. The other two appear in later chapters.
Strict JSON rules still apply
JSON-LD is parsed as JSON first. A syntax error makes the whole block unreadable, and consumers usually skip it without warning. These mistakes are the most common:
- A trailing comma after the last property.
- Single quotes instead of double quotes.
- Curly "smart" quotes pasted from a word processor.
- Comments, which JSON does not allow.
- A double quote inside a string that is not escaped as
\".
Reading a block
When you meet an unfamiliar JSON-LD block, read it in this order:
- Find
@contextto see which vocabulary applies. - Find
@typeto see what the object describes. - Read the other keys as properties of that type.
Exercise
This block fails to parse. Name the two syntax errors. {"@context": "https://schema.org", '@type': "Person", "name": "Ana Torres",}
Show answer
The key '@type' uses single quotes, but JSON requires double quotes. There is also a trailing comma after the "name" property. Fixing both gives {"@context": "https://schema.org", "@type": "Person", "name": "Ana Torres"}.