JSON.stringify()
If we need to send data to a web server the data should be a string.To change the object literal to string JSON.stringify() method should be used.For instance
Example-1
<html> <body> <p id="demo"></p> <script> var obj = {name:"rekha", age:40, city:"newyork"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON; </script> </body> </html>
Output
{"name":"rekha","age":40,"city":"newyork"}
Example-2
<html> <body> <p id="demo"></p> <script> var obj = {"name":"ramesh","age":30,"family": [{"mother":"geetha","father":"rao"}],"city":"berlin"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON; </script> </body> </html>
Output
{"name":"ramesh","age":30,"family":[{"mother":"geetha","father":"rao"}],"city":"berlin"}