JSON.parse() Explained

What JSON.parse() does, how to use it, and how to handle the errors it throws — with practical examples in JavaScript, Python, and PHP.

What does JSON.parse() do?

JSON.parse() takes a JSON string and converts it into a real JavaScript value — an object, array, number, string, boolean, or null. It is the opposite of JSON.stringify(), which turns a JavaScript value back into a JSON string.

You reach for it whenever data arrives as text — a response from an API, a value read from localStorage, or the contents of a .json file — because you cannot access properties on a string. Parsing turns that text into a structure you can actually work with.

Syntax

JSON.parse(text)
JSON.parse(text, reviver)
  • text — the JSON string to parse.
  • reviver (optional) — a function that transforms each value as it is parsed.

Basic example

Pass a JSON string and get back an object you can read from:

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

console.log(user.name);  // "Ada"
console.log(user.age);   // 36
typeof user;             // "object"

Arrays and nested structures work the same way:

const data = JSON.parse('[1, 2, {"ok": true}]');
data[2].ok;  // true

The reviver function

The optional second argument lets you transform values during parsing. A common use is converting date strings back into Date objects:

const text = '{"event":"launch","date":"2026-06-28T10:00:00Z"}';

const obj = JSON.parse(text, (key, value) => {
  if (key === 'date') return new Date(value);
  return value;
});

obj.date instanceof Date;  // true

JSON.parse() vs JSON.stringify()

They are mirror images of each other — one decodes, the other encodes:

JSON.parse()

String → JavaScript value (decode)

JSON.parse('{"a":1}')

JSON.stringify()

JavaScript value → String (encode)

JSON.stringify({ a: 1 })

Handling errors

If the string is not valid JSON, JSON.parse() throws a SyntaxError. Always wrap it in a try...catch when the input might be malformed:

function safeParse(text) {
  try {
    return JSON.parse(text);
  } catch (err) {
    console.error('Invalid JSON:', err.message);
    return null;
  }
}

The most frequent message is Unexpected token ... in JSON, usually caused by trailing commas, single quotes, unquoted keys, or an empty string. Our common JSON errors guide covers each one and how to fix it.

Decoding JSON in other languages

Every major language has an equivalent of JSON.parse() for decoding a JSON string:

Python

import json
data = json.loads('{"name": "Ada"}')
data["name"]  # "Ada"

PHP

$data = json_decode('{"name": "Ada"}', true);
echo $data["name"]; // Ada

Frequently asked questions

What does JSON.parse() return?

Whatever the JSON describes — an object, array, number, string, boolean, or null. It never returns the original string.

Does JSON.parse() modify the original string?

No. Strings are immutable in JavaScript; JSON.parse() reads the string and returns a brand-new value.

Why does JSON.parse() fail on a JavaScript object?

JSON.parse() expects a string. If you pass an object, JavaScript first converts it with toString() to [object Object], which is not valid JSON and throws. Only call it on strings.

Test your JSON before you parse it

Paste your JSON into our free formatter to validate it, spot the exact error, and auto-fix common mistakes — so JSON.parse() never trips on it.

Open JSON Formatter