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


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


const url = new URL("");
const obj = {key: url};

console.log(JSON.stringify(obj)); // logs '{"key": ""}'


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

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support ?1754 ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support ? ? ?54 ? ? ?

Document Tags and Contributors

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