Skip to content

JavaScript Tutorial JSON

JavaScript JSON.parse()

Lesson 132 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

JSON.parse() turns a JSON string into a real JavaScript value.

script.js
JS
const text = '{"name":"Maya","age":28,"skills":["js","css"]}';
const user = JSON.parse(text);

console.log(user.name);        // → Maya
console.log(user.skills[1]);   // → css
console.log(typeof user);      // → object

Parsing different values

script.js
JS
console.log(JSON.parse("42"));           // → 42
console.log(JSON.parse('"hello"'));      // → hello
console.log(JSON.parse("true"));         // → true
console.log(JSON.parse("null"));         // → null
console.log(JSON.parse("[1,2,3]"));      // → [1, 2, 3]

The input must be a string. JSON.parse(undefined) and JSON.parse({}) throw errors, because they are converted to text that is not valid JSON.

Wrap it in try…catch

If the text is not valid JSON, parse throws a SyntaxError. Always protect parsing of data you did not create, such as responses and stored values.

script.js
JS
function safeParse(text, fallback = null) {
  try {
    return JSON.parse(text);
  } catch {
    return fallback;
  }
}

console.log(safeParse('{"ok":true}'));       // → { ok: true }
console.log(safeParse("not json", {}));      // → {}

Dates are strings

JSON has no date type. Dates arrive as text, and you convert them.

script.js
JS
const data = JSON.parse('{"createdAt":"2026-03-15T10:30:00.000Z"}');
console.log(typeof data.createdAt);              // → string
console.log(new Date(data.createdAt).getUTCFullYear()); // → 2026

The reviver function

JSON.parse(text, reviver) lets you transform values while parsing.

script.js
JS
const text = '{"name":"Maya","joined":"2026-03-15T10:30:00.000Z"}';

const user = JSON.parse(text, (key, value) =>
  key === "joined" ? new Date(value) : value
);

console.log(user.joined instanceof Date); // → true

Parsing is not evaluating

JSON.parse only reads data. It never runs code, which is why it is safe, unlike the old eval() trick. Never use eval() to parse JSON.

Large numbers

Numbers beyond Number.MAX_SAFE_INTEGER lose accuracy when parsed. APIs often send very large IDs as strings for this reason, see BigInt.

lightbulb
Next
The other direction is JSON.stringify(). To get JSON from a server, see fetch, where response.json() parses it for you.