Full answer
Structured data describes the meaning of page content in a form that programs can read. Search engines and other tools use it, for example to show a course with a rating, an event with a date, or a product with a price. The shared vocabulary is called schema.org.
There are two common formats.
JSON-LD is a script element with type="application/ld+json" that holds a JSON object. It is separate from the visible markup, so it is easy to write, generate and change. The keys @context and @type name the vocabulary and the kind of thing. The browser does not run it, because the type is not a script type. Most search engine guides prefer this format.
Microdata puts the data into the visible markup with attributes: itemscope starts a thing, itemtype names its type, and itemprop marks each property. The values come from the text of the elements, or from attributes such as content and href. It stays in step with the visible text, but it is mixed into the HTML and harder to maintain.
Rules that matter in both:
- Mark up only what is visible on the page. Data that users cannot see can lead to penalties from search engines.
- Valid JSON is needed: no comments and no trailing commas. A syntax error means the data is ignored.
- Structured data does not guarantee a special search result. It makes the page eligible.
- Check pages with the validators that search engines provide.
A script can read the JSON-LD with JSON.parse and gets an object whose type is Course. The microdata version gives the same name through the itemprop elements. The JSON-LD block is not drawn on the page.
Keep the structured data in step with the text that users see.
<script type="application/ld+json" id="ld">
{
"@context": "https://schema.org",
"@type": "Course",
"name": "HTML and CSS basics",
"description": "Start with tags and styles.",
"provider": { "@type": "Organization", "name": "Schoolabe" }
}
</script>
<div itemscope itemtype="https://schema.org/Course" id="md">
<h2 itemprop="name">HTML and CSS basics</h2>
<p itemprop="description">Start with tags and styles.</p>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Structured data uses the schema.org vocabulary.
- JSON-LD is a script block with JSON.
- Microdata uses itemscope, itemtype and itemprop.
- Mark up only visible content.
- Invalid JSON means the data is ignored.
Common mistakes
- Marking up content that is not visible on the page.
- Writing invalid JSON in a JSON-LD block, such as a trailing comma.
- Expecting structured data to force a rich result.
Follow-ups
- Why does the browser not run a JSON-LD script?
- What is schema.org?
Tips
- Describe both formats briefly and say which one you prefer and why.
- Mention the rule: mark up only visible content.