Structured data
What you need to know
Structured data describes visible page content in a machine-readable format. JSON-LD is JSON placed inside a script element with type application/ld+json; it must accurately describe what users can see.
By the end, you can: describe real content with schema; serialize without unsafe HTML injection.
Serialize a small Book object safely
function jsonLd(value:object):string { return JSON.stringify(value).replace(/</g,'\\u003c');}const data={'@context':'https://schema.org','@type':'Book',name:'River Walks'};console.log(JSON.parse(jsonLd(data)).name);Expected behavior: River Walks. Escaping is reversible at the JSON layer; the literal HTML text no longer contains less-than characters.
How it works
Choose a schema type that matches the actual page, such as Book for a public guide. Add only facts that are available and truthful; do not invent review counts, ratings, prices or availability. JSON.stringify produces JSON but does not make the result safe for insertion into HTML. In a raw script context escape less-than characters as a JSON Unicode escape before insertion, so text data cannot introduce an HTML tag boundary. Prefer a small typed data object from validated public fields rather than arbitrary input. In React, use a raw HTML insertion only for this carefully serialized script; render ordinary page text normally. Structured data eligibility depends on search-engine policies and supported features; valid JSON-LD does not guarantee a rich result or ranking. Check the script content parses as JSON, equals the intended object and matches the visible page. Test actual emitted HTML separately. Do not add schema fields for private information or pages that are not genuinely published.
Insert reviewed structured data
export default function Page(){ const data={'@context':'https://schema.org','@type':'Book',name:'River Walks'}; const serialized=JSON.stringify(data).replace(/</g,'\\u003c'); return <><h1>River Walks</h1><script type='application/ld+json' dangerouslySetInnerHTML={{__html:serialized}}/></>;}Expected behavior: A compatible server render should emit one JSON-LD script describing the same visible title.
Common mistake
Do not pass user text directly to dangerouslySetInnerHTML. Do not add made-up reviews or claim a rich result is guaranteed.
Practice and verification
Use a title containing a literal less-than sign as benign input. Check serialization contains no literal less-than characters, JSON.parse restores the exact title, and the actual page still shows correct text.
Scope: Proposed Next.js 16.x / React 19, Cache Components disabled unless noted. Framework and browser examples are staged; local standalone tests are reported separately. Exact dependency patches and platform contracts pending.
Further reading: Jsonld · Searchguide