JavaScript JSON parse() Method

Last 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.

  • Converts JSON text into JavaScript values.
  • Throws a SyntaxError when the input contains invalid JSON.
  • Accepts an optional reviver function to transform parsed values.

Syntax:

JSON.parse(text, reviver);
  • text: The JSON string to parse.
  • reviver (optional): A function that transforms values during parsing.

Example: Converting a JSON String into a JavaScript Object

JavaScript
const s = '{"name": "Ryan", "age": 25, "city": "Delhi"}';

const obj = JSON.parse(s);

console.log(obj);
console.log(obj.name);

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 Function

The 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.

JavaScript
const s = '{"name": "Ryan", "age": 25, "city": "Delhi"}';

const obj = JSON.parse(s, (key, value) => {
  if (key === "age") {
    return value + 1;
  }
  return value;
});

console.log(obj);

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.

JavaScript
fetch("https://api.example.com/user")
  .then(response => response.json())
  .then(data => {
    console.log(data.name);
  })
  .catch(error => console.error("Error:", error));

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.

JavaScript
const user = { name: "Rahul", age: 25 };

localStorage.setItem("user", JSON.stringify(user));

const storedData = localStorage.getItem("user");

if (storedData !== null) {
  const obj = JSON.parse(storedData);
  console.log(obj.name);
}

3. Reading Configuration Data

JSON configuration data can be parsed into a JavaScript object to access settings such as themes, languages, and application preferences.

JavaScript
const s = '{"theme": "dark", "language": "en"}';

const config = JSON.parse(s);

console.log(config.theme);
console.log(config.language);

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.

JavaScript
const s = "{name: 'Ryan', age: 25}";

try {
  const obj = JSON.parse(s);
  console.log(obj);
} catch (error) {
  console.log("Invalid JSON:", error.message);
}

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.

JavaScript
const value = 12345;

try {
  const result = JSON.parse(value);
  console.log(result);
} catch (error) {
  console.log("Error:", error.message);
}
Comment