The Wayback Machine - https://web.archive.org/web/20240901184910/https://www.geeksforgeeks.org/what-is-difference-between-json-parse-and-json-stringify-methods-in-javascript/
Open In App

What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?

Last Updated : 06 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development.

JSON.parse() Method

JSON.parse() converts a JSON string to a JavaScript object. It accepts a JSON string as input. It must be in string format when sending data to a web server locally. It’s useful for storing data in local storage as browsers store data in key-value pairs.

Syntax

JSON.parse( string, function )

Example: In this example, we define a constant myInfo containing JSON-like data. It’s parsed into an object Obj. The code then logs the values of Name and Age from Obj. The resulting output would be GFG and 22, respectively.

Javascript




const myInfo = `{
   "Name": "GFG",
   "Age":22,
   "Department" : "Computer Science and Engineering",
   "Year": "3rd"
}`
 
const Obj = JSON.parse(myInfo);
console.log(Obj.Name) 
console.log(Obj.Age)


Output

GFG
22

JSON.stringify() Method

JSON.stringify() converts JavaScript objects into JSON strings, accepting a single object argument. It contrasts JSON.parse(). With replacer parameters, logic on key-value pairs is feasible. Date formats aren’t allowed in JSON; thus, they should be included as strings.

Syntax

JSON.stringify(value, replacer, space);

Example: This example we converts the JavaScript object myInfo into a JSON string using JSON.stringify(). It then logs the resulting JSON string, which represents the object’s data.

Javascript




const myInfo = {
   Name: "GFG", 
   Age:22,
   Department : "Computer Science and Engineering",
   Year: "3rd"
}
const Obj = JSON.stringify(myInfo);
console.log(Obj)


Output

{"Name":"GFG","Age":22,"Department":"Computer Science and Engineering","Year":"3rd"}

Difference Between JSON.stringify() Method & JSON.parse() Method

JSON.parse() JSON.stringify()
Converts JSON string to JavaScript object. Converts JavaScript object to JSON string.

The string must be wrapped in double quotes. example:  “String”.

No need to wrap the string in double quotes.

Accepts only one argument, the JSON string. Accepts only one argument, the JavaScript object.
Useful for parsing data received from servers. Useful for converting JavaScript objects for transmission.
Can handle JSON with nested objects and arrays. Preserves data types, including numbers, strings, arrays, and objects.
Can’t include functions or undefined values. Includes all enumerable properties, excluding functions and undefined values.
Handles dates as strings; must be converted. Allows optional replacer function to customize output or filter properties.


Similar Reads

How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
JavaScript JSON stringify() Method
The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object. Syntax: JSON.stringify(value, replacer, space);Parameters: value: It is the value that is to be converted into a JSON string.replacer: It is
3 min read
How to JSON Stringify an Array of Objects in JavaScript ?
In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects. Table of Content Using JSON.stringify with a Replacer Function Using a Custom Function for JSON StringifyUsing a Custom
3 min read
Node.js querystring.stringify() Method
The querystring.stringify() method is used to produce an URL query string from the given object that contains the key-value pairs. The method iterates through the object's own properties to generate the query string. It can serialize a single or an array of strings, numbers, and booleans. Any other types of values are coerced to empty strings. Duri
2 min read
JavaScript JSON parse() Method
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object. Syntax: JSON.parse( string, function ) Parameters: This method accepts two parameters as mentioned above and described below: string: It is a required parameter and it contains a string that is written in JSON form
2 min read
JavaScript SyntaxError - JSON.parse: bad parsing
This JavaScript exception thrown by JSON.parse() occurs if string passed as a parameter to the method is invalid. Message: SyntaxError: JSON.parse: unterminated string literal SyntaxError: JSON.parse: bad control character in string literal SyntaxError: JSON.parse: bad character in string literal SyntaxError: JSON.parse: bad Unicode escape SyntaxEr
2 min read
How does the JSON.parse() method works in JavaScript ?
The JSON.parse() method in JavaScript is used to parse a JSON (JavaScript Object Notation) string and convert it into a JavaScript object. JSON is a lightweight data-interchange format that is easy for humans to read and write, and it is also easy for machines to parse and generate. Example: Here, JSON.parse() take a JSON-formatted string (jsonStri
1 min read
How to Catch JSON Parse Error in JavaScript ?
JSON (JavaScript Object Notation) is a popular data interchange format used extensively in web development for transmitting data between a server and a client. When working with JSON data in JavaScript, it's common to parse JSON strings into JavaScript objects using the JSON.parse() method. However, parsing JSON data can sometimes lead to errors, e
1 min read
How to Parse JSON in JavaScript ?
Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipulation and retrieval in your JavaScript projects. Be
2 min read
How to Parse JSON Data in JavaScript ?
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write. It is easy for machines to parse and generate it. In JavaScript, parsing JSON data is a common task when dealing with APIs or exchanging data between the client and server. Table of Content Using JSON.parse() MethodUsing eval() Func
4 min read
How to universally parse JSON into blocks using jQuery ?
In jQuery, to parse any data to any block is carried over by using DOM Insertion methods. Some of DOM Insertion methods are append(), appendTo(), html(), prepend(), prependTo(), text(). To parse JSON into any block is also handled in same way but along with Ajax callback functions and parse.JSON() methods. Here parse.JSON() methods is deprecated in
3 min read
Moment.js Parse ASP.NET JSON Date
Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with a string to parse dates in ASP.NET JSON format. The moment() function is used to create a moment using a string representing a date in ASP.NET JSON format. Syntax: moment( String ) Parameters: It takes a st
1 min read
How to parse JSON Data into React Table Component ?
In React parsing JSON Data into React Table Component is a common task to represent data by building UI components. Prerequisites:NPM & Node JSReact JSJSON parse()Approach:We will create react table components and parse a JSON file consisting of objects with id, name, and city associated uniquely. We will use the .map() function to parse each o
2 min read
How to Parse JSON using Node.js?
JSON stands for JavaScript Object Notation. The text-based data exchange format data exchange format allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. JSON parsing is a common task when working with data from APIs, configuration files, or databases. In this article, we wi
2 min read
How to parse JSON string in Typescript?
In this tutorial, we will learn how we can parse a JSON string in TypeScript. The main reason for learning about it is to learn how we can explicitly type the resulting string to a matching type. The JSON.parse() method will be used to parse the JSON string by passing the parsing string as a parameter to it. Syntax:JSON.parse(parsingString);We can
4 min read
How to parse a JSON File in PHP?
In this article, we are going to parse the JSON file by displaying JSON data using PHP. PHP is a server-side scripting language used to process the data. JSON stands for JavaScript object notation. JSON data is written as name/value pairs. Syntax:{ "Data": [ { "key1": "value1", "key2": 123, "key3": "value3" }, { "key1": "value4", "key2": 456, "key3
2 min read
Why does Vite create multiple TypeScript config files: tsconfig.json, tsconfig.app.json and tsconfig.node.json?
In Vite TypeScript projects you may have noticed three typescript configuration files, tsconfig.json, tsconfig.app.json, and tsconfig.node.json. Each file is used for a different purpose and understanding their use cases can help you manage TypeScript configurations for your project. In this article, you will learn about the three TypeScript config
6 min read
What is the difference between the $parse, $interpolate and $compile services?
In AngularJS applications, the $parse, $interpolate and $compile services are the most important components as these services are used for data binding. Here, the $parse is mainly responsible for parsing and evaluating the expressions in the context of the application's scope. The $interpolate handles the interpolation of expressions and the $compi
6 min read
Difference between package.json and package-lock.json files
In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node. In Node, package.json is a versioning file used to install multiple packages in your project. As you initialize your node application, you will see three files installed in your app that is node_modules, package.json, and package
3 min read
How to parse a URL into hostname and path in javascript ?
We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the current web page. C/C++ Code <body style="
1 min read
How to parse HTTP Cookie header and return an object of all cookie name-value pairs in JavaScript ?
To parse HTTP Cookie header we will spilt the cookies data and create objects from the key-value extracted from cookies. Cookies are simply small text files that a web server sends to the user's browser. They contain the following data. Name-value pair with actual data.The expiry date for when the cookie becomes invalid.Domain and path of the serve
3 min read
How to parse URL using JavaScript ?
Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method to create a HTML element, anchor tag and then use
2 min read
How to Parse XML in JavaScript?
Parsing XML data is important because it allows JavaScript applications to extract structured information from XML documents. We will explore two different approaches to Parse XML in JavaScript. Below are the approaches to parsing XML in JavaScript: Table of Content Using DOM ParserUsing xml2js LibraryUsing DOM ParserIn this approach, we are using
2 min read
JavaScript Date parse() Method
The JavaScript Date parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970, UTC. If the argument is an invalid date string, it returns NaN (Not a Number). Syntax:Date.parse(datestring);Parameters:This method accepts a single parameter: datestring: This parameter holds the d
3 min read
How to Parse S-expressions in JavaScript?
The most simple and powerful notation used in Lisp programming is S-expressions and they are composed of atoms, say numbers or symbols or strings and lists (which too are S-expressions) parsing S-expressions in JavaScript can be done using a few basic techniques. What are S-expressions?To represent nested list data structures, we use s-expressions,
4 min read
How to parse float with two decimal places in JavaScript ?
Parsing a float with two decimal places in JavaScript refers to converting a string or number into a floating-point number while ensuring that it displays only two digits after the decimal point. This is commonly used for precise numerical formatting in web applications. We have different methods to parse float with two Decimal places in javascript
3 min read
What is the difference between find() and filter() methods in JavaScript ?
In this article, we will see the difference between the find() and filter() methods in javascript. JavaScript find() method: The find() method is used to find all the descendant elements of the selected element. It finds the element in the DOM tree by traversing through the root to the leaf. Syntax: array.find(function(currentValue, index, arr),thi
2 min read
Difference between preventDefault() and stopPropagation() methods in JavaScript
In this article, we will be discussing the PreventDefault & stopPropagation methods with suitable code examples for each condition & then we will see the difference between the PreventDefault vs stopPropagation. JavaScript preventDefault() Method: It is a method present in the event interface. This method prevents the browser from executing
3 min read
Difference between Set.clear() and Set.delete() Methods in JavaScript
Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes empty and the size of the set becomes 0. Syntax: M
2 min read
What is the difference between every() and some() methods in JavaScript ?
In this article, we will see the difference between every() and some() methods in JavaScript. Array.every() methodThe Array.every() method in JavaScript is used to check whether all the elements of the array satisfy the given condition or not. The output will be false if even one value does not satisfy the element, else it will return true, and it
3 min read