A very brief intro on JSON
JSON = JavaScript Object Notation
JSON is just a text format to store data on your server and transport it to the user easily.
Why do we need JSON.stringify()?
To transfer data to a web server, it requires a string conversion if the data is not in a string format.
JSON.stringify() is used to convert the JavaScript object into a string.
How to stringify a JavaScript Object?
Here’s an example of a JavaScript object:
const object1 = {name: "ABC", job: "Developer", place: "London"};
This is how you can convert it to a string:
const str = JSON.stringify(object1);
This is how the final string will look like and sent over to a web server:
{"name":"ABC","job":"Developer","place":"London"};
How to stringify a JavaScript Array?
Here’s an example of a JavaScript array:
const array1 = ["ABC", "DEF", "XYZ"];
This is how you can convert it to a string:
const str = JSON.stringify(array1);
This is how the final string will look like and sent over to a web server:
["ABC","DEF","XYZ"]
Date objects are not allowed in JSON.
JSON.stringify() will convert the date object into a string which cab be converted back to the date when you receive it.
Functions will be removed from the JS object when you use JSON.stringify().
References
array convert functions JSON string stringify