Sometimes, we want to serialize JavaScript object into JSON string.
In this article, we’ll look at how to serialize JavaScript object into JSON string.
How to serialize JavaScript object into JSON string?
To serialize JavaScript object into JSON string, we use the JSON.stringify
method.
For instance, we write
const myObject = new MyClass("1", "text");
const dto = { MyClass: myObject };
console.log(JSON.stringify(dto));
to call JSON.stringify
with dto
as the argument.
Then a JSON string if returned with the dto
object as the string content.
Conclusion
To serialize JavaScript object into JSON string, we use the JSON.stringify
method.