JSON.parse()

Beginner
⏱️ 8 min read
📚 Updated: May 2026
JavaScript built-in

What you’ll learn

JSON is how browsers, servers, and tools exchange structured data as text. In JavaScript, JSON.parse() is the standard way to turn that text into values you can read and manipulate.

This guide covers the method signature, a plain example, the optional reviver, and how to catch SyntaxError when the input is not valid JSON.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, language-independent text format for data interchange. It is easy for humans to read and for machines to parse. The format is built on two structures:

  • A collection of name/value pairs — in JavaScript this becomes an object (and similar types in other languages).
  • An ordered list of values — in JavaScript this becomes an array.

Example JSON

Here is a single JSON object that mixes strings, numbers, booleans, arrays, and nested objects — the kind of payload you often receive from an HTTP API.

JSON
{
  "name": "John Doe",
  "age": 30,
  "isStudent": false,
  "courses": ["Math", "Science", "History"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "zipcode": "12345"
  }
}

What JSON.parse() does

The JSON.parse() method parses a string as JSON and produces the corresponding JavaScript value. Use it when you have JSON text (for example from fetch().then(r => r.text()) or a file) and need an object or array in memory.

Syntax

JavaScript
JSON.parse(text[, reviver])
  • text — The string to parse. It must be valid JSON.
  • reviver (optional) — A function called for each key–value pair while the result is built; return the value you want in the final tree (or undefined to drop a property).
1

Basic example

A JSON string becomes a JavaScript object; properties are accessed with dot or bracket notation.

JavaScript
const jsonString = '{"name": "Jane Doe", "age": 25, "isStudent": true}';
const jsonObject = JSON.parse(jsonString);

console.log(jsonObject.name); // "Jane Doe"
console.log(jsonObject.age); // 25
console.log(jsonObject.isStudent); // true
2

Using a reviver function

The reviver runs during parsing so you can coerce types — here an ISO date string becomes a Date instance.

JavaScript
const jsonString = '{"name": "Jane Doe", "birthDate": "1990-01-01T00:00:00.000Z"}';

const jsonObject = JSON.parse(jsonString, (key, value) => {
  if (key === "birthDate") {
    return new Date(value);
  }
  return value;
});

console.log(jsonObject.birthDate instanceof Date); // true
console.log(jsonObject.birthDate.toISOString()); // "1990-01-01T00:00:00.000Z"
3

Error handling

If the string is not valid JSON, JSON.parse throws SyntaxError. Wrap parsing in try/catch so a bad payload does not crash your app. The exact message text depends on the JavaScript engine.

JavaScript
const jsonString = '{"name": "John Doe", "age": 30'; // invalid: missing closing brace

try {
  const jsonObject = JSON.parse(jsonString);
} catch (e) {
  console.error("Parsing failed:", e.name, e.message);
}

Untrusted input

Parsing succeeds only if the text is JSON; it does not replace validation. For data from users or third-party APIs, check required fields, types, and reasonable size before trusting the object — especially before spreading into state or passing to sensitive APIs.

Key takeaways

1

JSON.parse(text) converts a JSON string into a JavaScript object, array, or primitive.

2

The optional reviver lets you transform values while the tree is constructed (dates, enums, omitting keys).

3

Invalid JSON throws SyntaxError; use try/catch and validate payloads you did not author.

Frequently asked questions

A JavaScript value matching the JSON: object, array, string, number, boolean, or null. It does not invent undefined for absent keys; those keys are simply missing on the object.
No. Trailing commas after the last property or array element make the text invalid JSON and cause SyntaxError.
It is an optional (key, value) => newValue callback invoked while the parsed structure is built. Common uses include parsing date strings into Date objects or filtering sensitive fields.
No. eval runs arbitrary JavaScript. JSON.parse only accepts JSON text and is the correct, safe API for deserializing JSON.

Next: JSON.stringify()

After you can read JSON, learn the paired API that writes JSON from JavaScript values.

JSON.stringify() →
Did you know?

JSON.parse is not the same as eval: it only accepts JSON text and returns plain objects, arrays, and primitives. You should still validate parsed data from untrusted sources (shape, types, size) before merging into application state.

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.

10 people found this page helpful