Functions

JSON-LD serialization

Serialize a validated public structured-data object before inserting it as JSON-LD script text. Escape every less-than character to prevent a raw closing script tag. The helper also rejects values that cannot be represented as a JSON string; cyclic objects throw. It is not a general HTML sanitizer, a schema validator or a guarantee of search features. Source: https://nextjs.org/docs/app/guides/json-ld. Documentation-reviewed illustrative draft; installed declarations and runtime not verified.

Formal Syntax & Structure

nextjs syntaxnextjs
JSON.stringify(value).replace(/</g, '\\u003c')

Working Example

Example Snippetnextjs
function jsonLd(value:object):string { const text=JSON.stringify(value); if(typeof text!=='string') throw new TypeError('Expected JSON-serializable public data'); return text.replace(/</g,'\\u003c');}const data={'@context':'https://schema.org','@type':'Book',name:'River Walks'};console.log(JSON.parse(jsonLd(data)).name);
Next.js Reference