Sometimes, we want to create JSON string in JavaScript.
In this article, we’ll look at how to create JSON string in JavaScript.
How to create JSON string in JavaScript?
To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.
For instance, we write
const obj = {};
obj.name = "bob";
obj.age = 32;
obj.married = false;
const string = JSON.stringify(obj);
to create the obj object.
Then we add a few properties into it with
obj.name = "bob";
obj.age = 32;
obj.married = false;
Then we call JSON.stringify with obj to convert obj to a JSON string.
Conclusion
To create JSON string in JavaScript, we can create an object and then call JSON.stringify with it.
