JavaScript JSON.parse() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

In JavaScript, JSON.parse() reads a JSON string and turns it into a normal value you can use in code: an object, an array, a string, a number, a boolean, or null.

You use it after you get text from an API, a file, or localStorage. If the text is not valid JSON, JSON.parse() throws a SyntaxError. You can pass an optional reviver function to change values while they are being built.

Syntax

Call JSON.parse with the text. Add a reviver only when you need to change values.

JavaScript
JSON.parse(text);
JSON.parse(text, reviver);
CallWhat it does
JSON.parse(text)Reads the JSON string and returns the matching JavaScript value.
JSON.parse(text, reviver)Same as above, then runs reviver(key, value) for each property so you can change or drop values.

Pair with JSON.stringify() when you need to go the other way: value to string.

Library declarations
JavaScript
// ECMAScript 5 (ES5)
JSON.parse(text)
JSON.parse(text, reviver)

// text    : a string of JSON text
// reviver : optional function (key, value) => any
// returns : object | array | string | number | boolean | null
// throws  : SyntaxError when text is not valid JSON

Parameters

ParameterDescription
textRequired. The JSON string to read. Keys and strings must use double quotes.
reviverOptional. A function (key, value). Return the new value, or return undefined to remove that property.

Return Value

Return ValueDescription
JavaScript valueAn object, array, string, number, boolean, or null that matches the JSON text.
SyntaxError (thrown)Thrown when the text is not valid JSON. Catch it with try...catch.

JavaScript Examples

Example 1 — Basic JSON.parse()

Turn a JSON string into an object and read a property.

JavaScript
const text = '{"name":"Ann","age":20}';
const user = JSON.parse(text);

console.log(user.name);
console.log(user.age);
console.log(typeof user);
Try It Yourself

How It Works

  1. text is a string that looks like JSON.
  2. JSON.parse(text) builds a plain object.
  3. You can use user.name and user.age like any other object.

Example 2 — Change Values with a Reviver

Convert an ISO date string into a real Date while parsing.

JavaScript
const text = '{"title":"Meetup","when":"2026-11-20T10:00:00.000Z"}';

const event = JSON.parse(text, function (key, value) {
  if (key === "when") {
    return new Date(value);
  }
  return value;
});

console.log(event.title);
console.log(event.when instanceof Date);
console.log(event.when.getUTCFullYear());
Try It Yourself

How It Works

  1. The reviver runs for every key, including a final call with an empty key for the root value.
  2. When key is "when", return a Date instead of the string.
  3. Other values pass through unchanged with return value.

Example 3 — Catch Bad JSON

Invalid JSON throws. Wrap the call in try...catch.

JavaScript
const bad = "{name:'Ann'}";  // single quotes are not JSON

try {
  const user = JSON.parse(bad);
  console.log(user.name);
} catch (err) {
  console.log("Error type: " + err.name);
  console.log("Could not parse the text.");
}

const good = '{"name":"Ann"}';
const user = JSON.parse(good);
console.log("Parsed name: " + user.name);
Try It Yourself

How It Works

  1. The first string uses single quotes, so it is not valid JSON.
  2. catch runs and prints the error name.
  3. The second string uses double quotes and parses cleanly.

Browser & Runtime Support

JSON.parse() is part of ES5. Every current browser and Node.js has it. No import is needed.

✓ ES5 · Widely supported

JSON.parse()

Supported in Chrome, Edge, Firefox, Safari, Opera, and Node.js for many years. Safe for everyday web and server code.

ES5 Standard since
Google Chrome All modern · Desktop & Mobile
Full support
Microsoft Edge All modern · Chromium and legacy
Full support
Mozilla Firefox All modern · Desktop & Mobile
Full support
Apple Safari All modern · macOS & iOS
Full support
Opera All modern · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView Supported · Chromium based
Supported
Node.js 0.10+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer IE 8+ · Legacy
Supported
JSON.parse() Excellent

Bottom line: Safe to use everywhere. Prefer JSON.parse() over eval() when you need to read JSON text.

Frequently Asked Questions

It reads a JSON string and builds the matching JavaScript value: an object, array, string, number, boolean, or null.
text is the JSON string to read. reviver is optional. It is a function that runs for each key and value so you can change or drop values while parsing.
The JavaScript value that the JSON text describes. Bad JSON does not return a value. It throws a SyntaxError instead.
The text is not valid JSON. Common mistakes are single quotes, trailing commas, comments, or undefined. JSON needs double quotes around keys and strings.
Pass a reviver. Check the key or the string shape, then return new Date(value) when it looks like a date.
No. JSON.parse() only accepts JSON. It does not run code. Prefer JSON.parse() for data from APIs and files.

Next: JSON.stringify()

Turn a JavaScript value into a JSON string you can send or store.

JSON.stringify() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful