How to Convert JS Object to JSON String

  313 views   4 months ago Javascript

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:

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

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles