JSON.parse()

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

JSON.parse() reads a JSON text string and returns the matching JavaScript value—object, array, string, number, boolean, or null.

It only accepts strict JSON (double-quoted keys/strings, no trailing commas, no comments). Invalid text throws SyntaxError. Use try/catch for network or user input.

Beginner path.

Parse valid text → handle errors → try a reviver if you need type tweaks. Next: JSON.stringify().

Syntax

JSON
JSON.parse(text)
JSON.parse(text, reviver)
  • text A string containing JSON. Must be valid JSON grammar.
  • reviver Optional (key, value) => … to transform or drop values while building the result.

Valid vs Invalid

Side by side: text JSON.parse accepts vs text that throws.

Valid — parses
JSON
{
  "name": "Ada",
  "age": 36
}

Double-quoted keys and strings. No trailing comma.

Invalid — SyntaxError
Not JSON
{
  name: 'Ada',
  age: 36,
}

Unquoted keys, single quotes, and a trailing comma all fail.

After Parse

JSON.parse checks syntax only. It does not check that fields exist or have the types your app expects.

For shape and types, use JSON Schema (or your own checks) on the value you got back—especially for untrusted API payloads.

JSON
{
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  }
}

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — Basic Parse

Turn a JSON string into an object and read its fields.

JSON
const text = '{"name":"Ada","age":36,"active":true}';
const data = JSON.parse(text);

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

How It Works

  1. text is a string of JSON.
  2. JSON.parse builds a plain object.
  3. Numbers and booleans keep their types after parse.

Example 2 — Catch SyntaxError

Invalid JSON throws—wrap parse when the text might be bad.

JSON
const bad = "{ name: 'Ada', }";

try {
  console.log(JSON.parse(bad));
} catch (err) {
  console.log(err.name);
  console.log("Could not parse JSON");
}
Try It Yourself

How It Works

  1. The string is not strict JSON.
  2. JSON.parse throws SyntaxError.
  3. catch lets you show a message instead of crashing.

Example 3 — Reviver

Transform values while parsing—here, turn an ISO date string into a Date.

JSON
const text = '{"name":"Ada","joined":"2024-01-15T00:00:00.000Z"}';

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

console.log(data.name);
console.log(data.joined instanceof Date);
console.log(data.joined.getUTCFullYear());
Try It Yourself

How It Works

  1. JSON has no Date type—dates arrive as strings.
  2. The reviver runs for each key/value pair.
  3. Return a new value to replace, or return value unchanged.

Frequently Asked Questions

A JavaScript value matching the JSON text: object, array, string, number, boolean, or null. Missing keys are simply absent—not undefined properties you must read.
No. Trailing commas, single-quoted strings, and // comments are invalid JSON. JSON.parse throws SyntaxError.
An optional function (key, value) that runs on each value while the result is built. You can transform types, drop keys (return undefined), or leave values unchanged.
No. eval runs any JavaScript. JSON.parse only accepts JSON text. Always use JSON.parse for API and file JSON—never eval.

Next: JSON.stringify()

Learn how to turn values back into JSON text.

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.

18 people found this page helpful