Home » Interview Q&A » Difference between JSON.stringify() and JSON.parse() in JSON – JavaScript

Difference between JSON.stringify() and JSON.parse() in JSON – JavaScript

Find asp.net MVC version used by application
NumberStyle in Parse() and TryParse() in C#

Difference between JSON.stringify() and JSON.parse() in JSON

Now a days all browsers supports JSON and in JSON we have stringify() and parse(), are most important methods which helps us to formatting of JSON. Let’s try to understand Difference between JSON.stringify() and JSON.parse() in JSON.

  1. stringify() is used to convert JSON object to JSON String. It serializes a JavaScript object into a JSON string.
  2. parse() is used to convert JSON string/Array object to JSON Object. It deserializes a JSON string into a JavaScript object.

Let’s try to understand with examples:

Example-1 : Convert JSON Object into JSON String and then convert JSON string into JSON Object.

Please Note JSON.parse() throws if the string passed to it has trailing commas.

 

Example-2 : Also convert array in to JavaScript Object

 

Example-3 : JSON.parse() with replacer

Here we have a object with name,age and favoriteFood, in which we would require string in upper case and number value to be added with 10.

 

Example-4 : JSON.stringify() with replacer

 Here we have a object with name,age and favoriteFood, in which we would require string in upper case and verify whether age is less than 100 or not. If age is greater than 100, should return value else discard age.

 

Example-5 : JSON.stringify() with replacer and spacer for formatting

Below example is same as Example-4 except we could add space or any other characters as shown in example. In this example we have added “—” to understand use of second argument along with spacer.

 

 

About

Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload CAPTCHA.




DISCLAIMER:
The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.