URL: toJSON() method

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2018.

Note: This feature is available in Web Workers.

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

The method is automatically called by JSON.stringify() when a URL object is encountered during JSON serialization. The serialized value can then be deserialized using the URL() constructor within the reviver of JSON.parse().

Syntax

js
toJSON()

Parameters

None.

Return value

A string.

Examples

Calling toJSON() directly

Calling toJSON() directly returns a string containing the URL.

js
const url = new URL(
  "https://developer.mozilla.org/en-US/docs/Web/API/URL/toString",
);

const json = url.toJSON();
console.log(json); // https://developer.mozilla.org/en-US/docs/Web/API/URL/toString
console.log(typeof json); // "string"

Serializing to a JSON string

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

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

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

json
"https://developer.mozilla.org/en-US/docs/Web/API/URL/toString"

Specifications

Specification
URL
# dom-url-tojson

Browser compatibility

See also