Hey guys in this post we will discuss converting javascript object into a JSON string.
Convert JSON String into a javascript object check here
Using JSON.stringify() method
We can use JSON.strigify()
method to convert a javascript object into a JSON string. We will pass the javascript object as a parameter to the stringify()
method.
Syntax:
JSON.stringify(javascript_object);
Complete Example
Consider the following employee object –
const employee = {
name: "Bushan",
age: 28,
location: "India"
}
Use JSON.stringify()
method to convert employee object into JSON string.
const employeeJSON = JSON.stringify(employee);
console.log("Employee JSON:", employeeJSON);
Output:
Employee JSON: {"name":"Bushan","age":28,"location":"India"}