How to Convert JS Object to JSON String
Topic: JavaScript / jQueryPrev|Next
Answer: Use the JSON.stringify()
Method
You can use the JSON.stringify()
method to easily convert a JavaScript object a JSON string.
Let's take a look at the following example to see how this basically works:
Example
Try this code »<script>
// Sample JS object
var obj = {name: "Martin", age: 30, country: "United States"};
// Converting JS object to JSON string
var json = JSON.stringify(obj);
console.log(json);
// Prints: {"name":"Martin","age":30,"country":"United States"}
</script>
See the tutorial on JavaScript JSON parsing to learn more about encode/decode JSON data.
Related FAQ
Here are some more FAQ related to this topic: