This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

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("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"}'


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

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support ? 54 (54) ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? ? 54.0 (54) ? ? ? ? ?

© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.