JavaScript

How to convert a JavaScript object to JSON string

You can use the JSON.stringify() method to easily convert a JavaScript object to JSON. Let’s try the example below, to see how it works:
 

How to convert a JavaScript object to JSON string
// JavaScript Object 
var obj = {name: "Yohan", age: 30, country: "USA"};

// Converting the JavaScript object to JSON
var json = JSON.stringify(obj);
 
console.log(json);

Output :

{"name":"Yohan","age":30,"country":"USA"}
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *