The toJSON() method of the URL interface returns a DOMString containing the whole URL. It returns the same string as the href property.

Description

This method is supposed to only be invoked by the JSON.stringify() method.

Examples

const url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toJSON");
const obj = {key: url};

console.log(JSON.stringify(obj)); // logs '{"key": "https://developer.mozilla.org/en-US/docs/Web/API/URL/toJSON"}'

Specifications

Specification Status Comment
URL
The definition of 'toJSON()' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome ? Edge Full support 17Firefox Full support 54IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 54Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

Document Tags and Contributors

Contributors to this page: connorshea, chrisdavidmills, ziyunfei
Last updated by: connorshea,