Back to Content

TrustedHTML: toJSON() method

files/en-us/web/api/trustedhtml/tojson/index.md

latest824 B
Original Source

{{APIRef("Trusted Types API")}}{{AvailableInWorkers}}

The toJSON() method of the {{domxref("TrustedHTML")}} interface returns a JSON representation of the stored data.

Syntax

js-nolint
toJSON()

Parameters

None.

Return value

A string containing a JSON representation of the stored data.

Examples

The constant escaped is an object created via the Trusted Types policy escapeHTMLPolicy. The toString() method returns a string to safely insert into a document.

js
const escapeHTMLPolicy = trustedTypes.createPolicy("myEscapePolicy", {
  createHTML: (string) => string.replace(/</g, "&lt;"),
});

const escaped = escapeHTMLPolicy.createHTML("");
console.log(escaped.toJSON());

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}