Sometimes we confused that when I have to use JSON.parse() and when JSON.stringify. But most of the time we have to use both of these.
Let's talk about these important topics-
JSON.parse()
JSON.parse() takes JSON string and transform it into JavaScript object.
Input
let userStringData = '{"name":"Hamid","email":"hamid@gmail.com","profession":"Doctor"}';
let userObjData = JSON.parse(userStringData);
console.log(userObjData);
Output
{ name: 'Hamid', email: 'hamid@gmail.com', profession: 'Doctor' }
JSON.stringify()
JSON.stringify() is total oposite of JSON.parse(). Where JSON.stringify takes JavaScript object and transform it into JSON string.
Input
let userObjectData =
{ name: 'Hamid', email: 'hamid@gmail.com', profession: 'Doctor' }
let userJSONData = JSON.stringify(userObjectData);
console.log(userJSONData);
Output
{"name":"Hamid","email":"hamid@gmail.com","profession":"Doctor"}
Follow me on LinkedIn
Top comments (0)