GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript JSON parse() Method - GeeksforGeeks JavaScript JSON parse() MethodLast Updated : 19 Oct, 2026 The JSON.parse() method converts a valid JSON string into a JavaScript value, such as an object or array. It is commonly used when processing JSON data from APIs, browser storage, and configuration files.
Syntax: JSON.parse(text, reviver);
Example: Converting a JSON String into a JavaScript Object
Output { name: 'Ryan', age: 25, city: 'Delhi' }
Ryan
In this example, JSON.parse() converts the JSON string into a JavaScript object, allowing its properties to be accessed using dot notation. Using the Reviver FunctionThe optional reviver function lets you transform values as the JSON string is parsed. It receives each property's key and value and returns the value to retain in the resulting structure.
Output { name: 'Ryan', age: 26, city: 'Delhi' }
The reviver increases the age value by one during parsing, so the resulting object contains age: 26. Common Use Cases of JSON.parse()1. Parsing API Data When working with APIs, response data is often provided as JSON. The Fetch API's response.json() method already parses the response body, so calling JSON.parse() again is unnecessary.
2. Retrieving Data from localStorage Browser localStorage stores values as strings. Use JSON.stringify() to store an object as JSON and JSON.parse() to convert the retrieved string back into a JavaScript value.
3. Reading Configuration Data JSON configuration data can be parsed into a JavaScript object to access settings such as themes, languages, and application preferences.
Output dark en Handling Common Errors with JSON.parse()1. Invalid JSON Format JSON requires double-quoted property names and string values. Invalid syntax causes JSON.parse() to throw a SyntaxError.
Output Invalid JSON: Expected property name or '}' in JSON at position 1 (line 1 column 2) The exact error message varies by JavaScript engine. The input is invalid because the property names and string value do not follow JSON syntax. 2. Non-String Input JSON.parse() converts its argument to a string before parsing it. Therefore, non-string inputs do not always throw an error; the result depends on the string representation of the value.
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |