Skip to content

stringifyForHtml

Serializes a value to compact JSON that is safe to inline in an HTML page, typically in a <script type="application/json"> block or a script assignment.

<, > and & are written as \u003c, \u003e and \u0026, so the text can never close the script element (</script>), open a comment (<!--) or be read as an entity. U+2028 and U+2029, valid in JSON but line terminators in some script contexts, are escaped too. These characters can only appear inside JSON strings, so the escaped text parses back to exactly the same value with JSON.parse.

It throws like stringify does (a TypeError for undefined, a function, a symbol, a BigInt or a circular structure).

Available since v3.1.5

import { stringifyForHtml } from '@helpers4/json';
// or, from the all-in-one package (same code, one install):
import { stringifyForHtml } from 'helpers4/json';
stringifyForHtml(value: unknown): string
ParameterTypeDescription
valueunknownThe value to serialize

string — Compact JSON text with no raw <, >, &, U+2028 or U+2029

A value containing can no longer close the element it is embedded in.

stringifyForHtml({ note: '</script><b>' })
// => '{"note":"\\u003c/script\\u003e\\u003cb\\u003e"}'

The escapes are plain JSON escapes: JSON.parse gives back the original value.

JSON.parse(stringifyForHtml({ a: '<&>' })).a
// => '<&>'

View source on GitHub