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:
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