Sub topic 9
Placing JSON-LD in a web page
This chapter assumes you can write basic HTML, read JSON, and know how @context and @type work. It covers where JSON-LD goes in a page, how the browser treats it, and the errors that stop it from being read.
The script element
JSON-LD lives in a script element with the type application/ld+json. Browsers do not run script elements with an unknown type. They ignore the content, and crawlers read it as data.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "How to Brew Cold Coffee",
"datePublished": "2026-03-14T08:00:00+00:00",
"image": "https://example.com/images/cold-coffee.jpg",
"author": {
"@type": "Person",
"name": "Ana Torres",
"url": "https://example.com/authors/ana"
}
}
</script>The content must be valid JSON. Do not add JavaScript syntax such as comments, trailing commas, single-quoted strings, or a var assignment.
Where to put it
You can place the element in the head or in the body. Both work, because crawlers read the whole document. Pick one place and use it across your site. The head keeps markup apart from the visible content. The body can sit next to the content it describes, which helps when a template renders one block per component.
You can include several script elements on one page. Each one is parsed on its own, so an error in one block does not break the others. Alternatively, put several entities into one block with @graph, as covered in the previous chapter.
Static and dynamic output
The markup can be in the HTML your server sends, or it can be added by JavaScript after the page loads. Google can read both, but server-rendered markup is more reliable. It does not depend on a script finishing, and simpler crawlers can still see it. Prefer server-side output when you can.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Block is ignored | Wrong `type`, such as `text/javascript` | Use `application/ld+json` exactly |
| Parse error | Trailing comma or unescaped quote | Run the block through a JSON linter |
| Markup ends early | A data value contains `</script>` | Escape `</` as `<\/` |
| Entity is missing | `@context` is absent | Add `"@context": "https://schema.org"` |
| Markup does not match the page | Block describes content the user cannot see | Keep markup and visible content in agreement |
Check your understanding
Exercise
A page includes <script type="text/javascript"> with a valid JSON-LD object inside it. A crawler finds no structured data. Why, and how do you fix it?
Show answer
The crawler looks for script elements with the type application/ld+json. The browser would also try to run this one as JavaScript, which fails. Change the type attribute to application/ld+json.
Exercise
One of three JSON-LD blocks on a page has a trailing comma. What happens to the other two, and why?
Show answer
They are still read. Each script element is parsed separately, so only the block with the invalid JSON is skipped.