Converting Between JSON and Javascript Object
You convert a Javascript object to JSON using JSON.stringify. This function is part of Javascript. You just need to pass it an Javascript object and it returns a string that is JSON. No extra library is needed.
You can convert a JSON string to a Javascript object using JSON.parse and passing into it the JSON string.
Here is an example where we have a Javascript object Employee. We stringify it and log out the JSON in the browser console.
This is the output to the browser console…
Note that there is quotes in the “firstName”. That is because to be valid JSON, the properties must be in quotes. A Javascript object does not need properties to be in quotes, but it is perfectly fine to do so. So JSON is a subset of Javascript.
Then we used JSON.parse to convert some JSON string to a Javascript object which we can use.