Node | URL.toJSON() Method
The url.toJSON() method in node.js URL module is used to return the serialized URL of the URL object. The return value of this method is equivalent to the URL.href and url.toString() method. If an URL object is serialized using JSON.stringify() method then it is called automatically.
Syntax:
url.toJSON()
Parameters: This method does not accept any parameters.
Return Value: This method returns the serialized URL of the URL object.
Below examples illustrate the url.toJSON() method in Node.js:
Example 1:
// node program to demonstrate the// url.toJSON() method in node.js // Require an URL moduleconst url = require('url'); // Creating and initializing myURL variablevar urls = []; // Display resultconsole.log(JSON.stringify(urls)); |
Output:
[
"https://www.geeksforgeeks.org/",
"https://www.google.com/",
"https://www.mygeeks.com/"
]
Example 2:
// node program to demonstrate the// url.toJSON() method in node.js // Require an URL moduleconst url = require('url'); // Creating and initializing myURL variablevar myurl = []; // Display resultconsole.log(JSON.stringify(myurl)); |
Output:
[
"https://www.geeksforgeeks.org/",
"https://www.contribute.geeksforgeeks.org/",
"https://www.practice.geeksforgeeks.org/",
"https://www.demo.geeksforgeeks.org/",
"https://www.write.geeksforgeeks.org/"
]
Note: The above program will compile and run by using the node index.js command.


