The Wayback Machine - https://web.archive.org/web/20240902191813/https://www.geeksforgeeks.org/how-does-the-json-parse-method-works-in-javascript/
Open In App

How does the JSON.parse() method works in JavaScript ?

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

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 (jsonString) and converts it into a JavaScript object (parsedObject). Once parsed, you can access the properties of the object as you would with any other JavaScript object.

Javascript




// A JSON string representing an object
const jsonString =
    '{"name": "John", "age": 30, "city": "New York"}';
 
// Using JSON.parse() to convert the JSON
// string into a JavaScript object
const parsedObject = JSON.parse(jsonString);
 
// Now parsedObject is a JavaScript
// object that you can work with
console.log(parsedObject.name); // Output: John
console.log(parsedObject.age);  // Output: 30
console.log(parsedObject.city); // Output: New York


Output

John
30
New York


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
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
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() MethodJSON.parse() converts a JSON string to a JavaScrip
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
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 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
Convert JSON String to Array of JSON Objects in JavaScript
Given a JSON string, the task is to convert it into an array of JSON objects in JavaScript. This array will contain the values derived from the JSON string using JavaScript. There are two approaches to solving this problem, which are discussed below: Table of Content Using JSON.parse() MethodUsing JavaScript eval() MethodUsing Function ConstructorU
4 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
Javascript - Catching error if json key does not exist
In this article, we will discuss the error handling when a key is not present inside the javascript object. A JSON is a string format that encloses a javascript object in double-quotes. Example: '{"site": "gfg"}' A JSON object literal is the javascript object present inside the JSON String. Example: {"site": "gfg"} The key/value pairs are present i
2 min read
How does Query.prototype.circle() works in Mongoose ?
The Query.prototype.circle() function is used to specify a $center or $centerSphere condition. It specifies a circle for a $geoWithin query.Syntax: Query.prototype.circle() Parameters: This function has one area parameter and one optional path parameter.Return Value: This function returns Query Object. Installing mongoose: npm install mongoose Afte
2 min read
How does Query.prototype.Symbol.asyncIterator() works in Mongoose ?
The Query.prototype.Symbol.asyncIterator() function is used to return an asyncIterator for use with for/await/of loops. And it works for find() queries. User does not need to call this function explicitly, it gets automatically called by the JavaScript runtime.Syntax: Query.prototype.Symbol.asyncIterator() Parameters: This function does not have an
2 min read
How does Query.prototype.maxTimeMS() works in Mongoose ?
The Query.prototype.maxTimeMS() function is used to set the maxTimeMS option. Basically this function tells the MongoDB server to abort if the query or write operation has been running for more than ms milliseconds. Syntax: Query.prototype.maxTimeMS() Parameters: This function has one ms parameter which defines the number of milliseconds.Return Val
2 min read
How does Query.prototype.map() works in Mongoose ?
The Query.prototype.map() function is used to run a function fn and treats the return value of fn as the new value for the query to resolve to. All the function which are passed to map() function will be run after any post hooks. Syntax: Query.prototype.map() Parameters: This function has one parameter fn which is function to run to transform the q
3 min read
How does Query.prototype.merge() works in Mongoose ?
The Query.prototype.merge() function is used to merge another Query or conditions object into the current query for which this function is used i.e this query. When a Query is passed, conditions, field selection and options are merged.Syntax: Query.prototype.merge() Parameters: This function has one source parameter of Object/Query type.Return Valu
3 min read
How does Query.prototype.lte() works in Mongoose ?
The Query.prototype.lte() function is used to specify a $lte query condition. It returns documents that are less than or equal to the specified condition. Syntax: Query.prototype.lte() Parameters: This function has val parameter and an optional path parameter.Return Value: This function returns Query Object. Mongoose Installation: npm install mongo
2 min read
How does Query.prototype.getPopulatedPaths() works in Mongoose ?
The Query.prototype.getPopulatedPaths() function is used to get a list of paths to be populated by this query. An array of strings representing populated paths is passed as a parameter. Syntax: Query.prototype.getPopulatedPaths() Parameters: This function has one array parameter which is an array of strings representing populated paths.Return Value
2 min read
How does Query.prototype.geometry() works in Mongoose ?
The Query.prototype.geometry() function is used to specify a $geometry condition. The geometry can be provided in an array which is passed as parameter. Syntax: Query.prototype.geometry() Parameters: This function has one object parameter which must contain a type property of string type and a coordinates property of array type. Return Value: This
2 min read
How does await and async works in ES6 ?
In this article, we will try to understand how exactly async and await works in ES6. Let us first try to understand what exactly async and await are along with their usage and then further we will see some examples in which we would see how we may use async and await along with the normal functions. Async and Await: Async and Await both are conside
3 min read
What is Reselect and how does it Works in React JS ?
In React JS, Reselect is a library that enhances the efficiency of data computation through selectors. Primarily used in conjunction with Redux, a state management library for React, Reselect facilitates the creation of memoized selectors. This article explores Reselect and its various functions, with a focus on the `createSelector` function. `crea
4 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 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