Skip to content

Convert the object to JSON JavaScript | Example code

  • by

Use the JSON stringify() method to easily convert a JavaScript object into a JSON string. This method in JavaScript allows us to take a JavaScript object or Array and create a JSON string out of it.

Example convert an object to JSON JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<head>

  <script>

    var obj = { "Name":"John", "Age":50, "City":"New York"};
    var myJSON = JSON.stringify(obj);

    console.log(myJSON)
    console.log(typeof(myJSON))

  </script>

</head>
</html>

Output:

Convert the object to JSON JavaScript

Do comment if you have any doubts or suggestions on this JS JSON object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading