GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — TrustedScriptURL: toJSON() method - Web APIs | MDN

TrustedScriptURL: toJSON() method

Baseline 2026
Newly available

Since February 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

  • See full compatibility
  • Learn more

Note: This feature is available in Web Workers.

The toJSON() method of the TrustedScriptURL interface returns a string representing the TrustedScriptURL object. This is the same string that TrustedScriptURL.toString() returns.

The method is automatically called by JSON.stringify() when a TrustedScriptURL object is encountered during JSON serialization. The serialized value can then be converted back using the TrustedTypePolicy.createScriptURL() method within the reviver of JSON.parse(), which runs the policy's transformation on it again.

Syntax

js
toJSON()

Parameters

None.

Return value

A string.

Examples

Calling toJSON() directly

This example creates a TrustedScriptURL object. Calling toJSON() directly returns its stored string value.

js
const sanitized = scriptPolicy.createScriptURL(
  "https://example.com/my-script.js",
);

const json = sanitized.toJSON();
console.log(json); // Same value as sanitized.toString()
console.log(typeof json); // "string"

Serializing to a JSON string

The TrustedScriptURL object from the previous example is serialized using JSON.stringify(), which calls the toJSON() method automatically.

js
console.log(JSON.stringify(sanitized));

This would log the following JSON string, including the surrounding double quotes, which are part of the string's content:

json
"https://example.com/my-script.js"

Specifications

Specification
Trusted Types
# dom-trustedscripturl-tojson

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on Oct 30, 2026 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.