When to use Stringify and when to use parse.

Shubham15
Kilo Contributor

Hii,

I have been developing ServiceNow projects and was using JSON.parse and stringify. But I am not exactly sure when to use them and what is the difference between them. 

Can anyone please brief me about it.

2 REPLIES 2

Surbhi Srivasta
Tera Expert

Hii Shubham,

They are the complete opposite of each other.

JSON.parse() is used for parsing data that was received as JSONit deserializes a JSON string into a JavaScript object.

JSON.stringify() on the other hand is used to create a JSON string out of an object or arrayit serializes a JavaScript object into a JSON string.

JSON.stringify(obj) - Takes any serializable object and returns the JSON representation as a string.

JSON.stringify() -> Object To String.

JSON.parse(string) - Takes a well formed JSON string and returns the corresponding JavaScript object.

JSON.parse() -> String To Object.

You can also refer to this link for more details:-
https://javascript.plainenglish.io/how-to-use-stringify-and-parse-in-javascript-6b637b571a32


Please mark my answer Helpful/Correct, if it is an impactful

Regards,
Surbhi

Vaishnavi Lathk
Mega Sage
Mega Sage

Hello,

stringify() takes a JavaScript object and then transforms it into a JSON string. JSON. parse() takes a JSON string and then transforms it into a JavaScript object.
for Realtime usecases :-
https://community.servicenow.com/community?id=community_blog&sys_id=aedbb9e8dbafbb805ed4a851ca9619ba
Please mark my answer Helpful/Correct, if it is an impactful
 
Regards,
Vaishnavi Lathkar