Sub topic 2
JSON-LD as JSON with meaning
This chapter assumes you can read and write JSON (objects, arrays, strings, numbers, booleans) and know what a script element is. You also need to know what a URL is. You don't need any prior knowledge of RDF or the semantic web.
The problem plain JSON leaves open
Consider this JSON:
{
"name": "Ana Torres",
"url": "https://example.com/authors/ana"
}A person reading it can guess that it describes someone called Ana Torres. A program can't. The key name could mean a person's name, a company name, a file name, or a product name. JSON defines how data is written. It says nothing about what the keys mean, so every consumer needs private knowledge of each producer's format.
JSON-LD (JSON for Linked Data) fixes this without changing the syntax. A JSON-LD document is a JSON document. It uses a small set of reserved keys, called keywords, to connect each key to a globally unique identifier. Any program that understands JSON-LD can then tell that your name means the same thing as another site's name.
What JSON-LD adds to JSON
Two ideas carry the whole format:
- Every key and every type name can be mapped to an IRI (Internationalized Resource Identifier, a URL-like string such as
https://schema.org/name). An IRI is a unique name. Nobody else uses that exact string for a different meaning. - The mapping is stored in a
@context. The context lets you write short names such asnamein your data while a processor knows the full identifier.
Here is the same data with meaning attached:
{
"@context": "https://schema.org",
"@type": "Person",
"name": "Ana Torres",
"url": "https://example.com/authors/ana"
}The @context value points to the schema.org vocabulary. The @type value says the object is a Person. The following chapters explain @context and @type in detail. For now, treat them as the two keywords that turn the object into a described thing.
Keywords
Any key that starts with @ is a JSON-LD keyword. Keywords are case-sensitive and processors reserve them, so you can't use them as ordinary property names. This table lists the ones you will meet in schema.org markup.
| Keyword | Purpose | Covered in |
|---|---|---|
| `@context` | Maps short terms to full IRIs | Declaring `@context` and `@type` |
| `@type` | Names the kind of thing an object describes | Declaring `@context` and `@type` |
| `@id` | Gives an object an identifier so others can refer to it | Naming entities with `@id` |
| `@graph` | Holds several top-level objects in one document | Connecting entities with `@graph` |
| `@value`, `@language` | Carry a literal value and its language | Language tags, lists, and sets |
| `@list`, `@set` | Mark ordered or unordered collections | Language tags, lists, and sets |
Everything else in a typical document is an ordinary key, such as name, price, or author. Its meaning comes from the context.
How a processor reads your document
A JSON-LD processor works in stages. It parses the text as JSON. It then applies the context to expand each short term into a full IRI. The result describes a graph of things and the relationships between them.
You can see the expanded form yourself. The Person above expands to this:
[
{
"@type": ["https://schema.org/Person"],
"https://schema.org/name": [{ "@value": "Ana Torres" }],
"https://schema.org/url": [{ "@value": "https://example.com/authors/ana" }]
}
]Notice three things about the expanded form:
- Every key is a full IRI, so no ambiguity remains.
- Every value is wrapped in an array, and each literal sits in an object with
@value. - The short form and the expanded form carry the same information. You write the short form. A processor compares documents in the expanded form.
This is why two sites can write different contexts or different key spellings and still say the same thing. After expansion, the IRIs match.
Rules inherited from JSON
Because a JSON-LD document must first be valid JSON, every JSON rule applies. Most failures in real markup come from breaking one of them.
- Keys and strings use double quotes. Single quotes are invalid.
- Trailing commas are invalid.
- Comments are invalid, including
//and/* */. - Numbers can't have leading zeros or a thousands separator.
- Only
true,false, andnullare literal keywords, written in lowercase.
You can check a block locally before you publish it:
import json
good = '{"@context": "https://schema.org", "@type": "Person", "name": "Ana Torres"}'
bad = '{"@context": "https://schema.org", "@type": "Person", "name": "Ana Torres",}'
print(json.loads(good)["@type"])
try:
json.loads(bad)
except json.JSONDecodeError as error:
print("Invalid JSON:", error)The first call prints Person. The second raises an error because of the trailing comma. This only tests JSON syntax. A document can pass this test and still be wrong as JSON-LD.
Properties of the format worth knowing
Key order does not matter
JSON objects are unordered, and JSON-LD follows that. Putting name before @type changes nothing. Put @context first by convention so that a human reader sees the vocabulary before the data.
One value and an array of one value are equivalent
These two lines say the same thing:
{ "@type": "Person", "name": "Ana Torres" }{ "@type": ["Person"], "name": ["Ana Torres"] }In JSON-LD, a property can always have several values. Writing a single value is a shorthand. This is why you can give an author as one object, or as an array of several objects, without changing any rules. Whether a consumer such as a search engine accepts several values for a given property is a separate question. That depends on the consumer.
Unknown terms disappear
If a key isn't defined by the context, a processor drops it when it expands the document. It doesn't report an error. This is the most common reason that a typo goes unnoticed:
{
"@context": "https://schema.org",
"@type": "Person",
"nmae": "Ana Torres"
}This is valid JSON and valid JSON-LD. After expansion, nmae has no IRI and is discarded, so the entity ends up with no name. Keys are case-sensitive, so Name is also not the same as name. A validator, covered in a later chapter, catches these mistakes. A JSON parser can't.
The document describes data, not layout
JSON-LD doesn't say where on a page the information appears or how it looks. It only states facts about things. This is also why markup must match what visitors can see on the page. The data can say anything, so search engines check it against visible content.
The media type
A JSON-LD file has the media type application/ld+json. The same string appears as the type attribute when you embed markup in HTML. You will use it in the chapter on placing JSON-LD in a web page.
Common mistakes at this stage
| Mistake | Effect | Fix |
|---|---|---|
| Trailing comma or comment | The block fails to parse | Validate with a JSON parser |
| Missing `@context` | Keys have no meaning, so a processor ignores them | Add `"@context": "https://schema.org"` |
| Misspelled or wrongly cased key | The key is dropped silently | Check names against the vocabulary |
| Using `@` in your own key names | Conflicts with reserved keywords | Choose names without a leading `@` |
| Treating JSON-LD as a different file format | Wrong tooling or media type | Remember it is JSON plus keywords |
Exercise
The following block is pasted into a page. It parses without error in a JSON parser, but a JSON-LD processor reports that the entity has no name. Give the reason, and say whether a trailing comma after "Ana Torres" would change the outcome. { "@context": "https://schema.org", "@type": "Person", "Name": "Ana Torres" }
Show answer
The key is "Name" with a capital N. Keys are case-sensitive, and schema.org defines "name" in lowercase, so "Name" has no IRI and the processor drops it during expansion. It is still valid JSON, so the parser doesn't complain. A trailing comma after "Ana Torres" would make things worse: the block would no longer be valid JSON, so nothing in it could be read at all.
Exercise
Explain in two or three sentences why the expanded form of a JSON-LD document makes it possible for two sites with different key spellings in their own contexts to describe the same fact.
Show answer
Expansion replaces every short term with the full IRI that the context maps it to. A processor compares documents after this step, not before it. If both sites map their terms to the same IRI, such as https://schema.org/name, the expanded documents contain the same identifiers and so state the same fact, whatever short names the authors wrote.