JSON.parse()

JSON.parse(text, reviver)
Returns: any · Updated March 13, 2026 · JSON
json parse serialization

JSON.parse() converts a JSON-formatted string into a JavaScript value. It is the standard way to deserialize data received from APIs, files, or localStorage. The method handles all valid JSON types: objects, arrays, strings, numbers, booleans, and null.

Syntax

JSON.parse(text)
JSON.parse(text, reviver)

Parameters

ParameterTypeDefaultDescription
textstringThe JSON string to parse. Must conform to valid JSON syntax.
reviverfunctionundefinedOptional function that transforms each value before returning it. Called with (key, value, context) for each property.

The reviver Function

When provided, the reviver function receives three arguments:

  • key: The property name (string), even for array indices
  • value: The parsed value
  • context: An object containing source — the original JSON text for primitive values

The reviver runs in a depth-first manner, processing nested values before their parents. If the reviver returns undefined, the property is removed from the result.

Examples

Parsing Basic Values

// Parsing an object
const data = JSON.parse('{"name": "Alice", "age": 30}');
console.log(data.name); // "Alice"
console.log(data.age);  // 30

// Parsing different JSON types
console.log(JSON.parse("{}"));              // {}
console.log(JSON.parse("true"));            // true
console.log(JSON.parse('"hello"'));         // "hello"
console.log(JSON.parse("[1, 2, 3]"));       // [1, 2, 3]
console.log(JSON.parse("null"));            // null

Using the Reviver Parameter

// Double all numbers during parsing
const result = JSON.parse(
  '{"a": 1, "b": 2, "c": 3}',
  (key, value) => {
    if (typeof value === "number") {
      return value * 2;
    }
    return value;
  }
);
console.log(result); // { a: 2, b: 4, c: 6 }

// Parsing dates stored as strings
const withDates = JSON.parse(
  '{"created": "2024-01-15T10:30:00Z"}',
  (key, value) => {
    if (key === "created") {
      return new Date(value);
    }
    return value;
  }
);
console.log(withDates.created); // 2024-01-15T10:30:00.000Z (Date object)

Handling Large Numbers Without Precision Loss

// JSON numbers lose precision beyond Number.MAX_SAFE_INTEGER
// Store as string, revive to BigInt
const bigNum = JSON.parse(
  '{"id": 9007199254740993}',
  (key, value, context) => {
    if (key === "id") {
      return BigInt(context.source);
    }
    return value;
  }
);
console.log(bigNum.id); // 9007199254740993n (BigInt)

Common Errors

// Trailing comma - throws SyntaxError
try {
  JSON.parse("[1, 2, 3,]");
} catch (e) {
  console.log(e.message);
  // "Unexpected token ] in JSON at position 11"
}

// Single quotes - throws SyntaxError
try {
  JSON.parse("{'foo': 1}");
} catch (e) {
  console.log(e.message);
  // "Unexpected token ' in JSON at position 1"
}

// Valid: use single quotes for JS string, double for JSON
console.log(JSON.parse('{"x": 1}')); // { x: 1 }

Common Patterns

Reading from localStorage

const stored = localStorage.getItem("userPreferences");
if (stored) {
  const prefs = JSON.parse(stored);
  // Use prefs object
}

Fetching and Parsing API Responses

async function fetchData(url) {
  const response = await fetch(url);
  const text = await response.text();
  return JSON.parse(text);
}

Safe Parsing with Default

function parseJSON(str, fallback = null) {
  try {
    return JSON.parse(str);
  } catch {
    return fallback;
  }
}

const data = parseJSON(invalidInput, { error: true });

Preserving Type Information for Round-Trips

// Serialize Map with type marker
const map = new Map([["key", "value"]]);
const json = JSON.stringify(map, (k, v) => 
  v instanceof Map ? { __type: "Map", entries: [...v] } : v
);

// Revive Map
const restored = JSON.parse(json, (k, v) => 
  v?.__type === "Map" ? new Map(v.entries) : v
);
console.log(restored); // Map { "key" => "value" }

See Also

  • JSON.stringify() — Convert JavaScript values to JSON strings
  • BigInt — Handle integers beyond Number.MAX_SAFE_INTEGER