The JavaScript JSON.parse() method
The JSON.parse() method turns a JSON string into a JavaScript value. JSON.parse('{"a":1}') gives the object {a: 1}. You use it to read data that arrived as text — from an API or localStorage. Wrap it in try/catch, because invalid JSON throws a SyntaxError. Its inverse is JSON.stringify().
Overview
JSON.parse() is how data comes into your program: it takes a JSON-formatted string and rebuilds it as a real JavaScript object or array you can work with. Anything stored as text — an API response read as a string, a value pulled from localStorage — needs parsing before you can access its properties.
It's the mirror image of JSON.stringify(): stringify goes object-to-string, parse goes string-to-object. Together they round-trip data through any text channel. Note that most fetch() code uses response.json() instead, which parses for you — you reach for JSON.parse() directly when you already hold a string.
The one rule you must not skip: wrap it in try/catch. If the string isn't valid JSON — a truncated response, an empty localStorage value, a stray trailing comma — JSON.parse() throws a SyntaxError and crashes that code path. Catching it lets you fall back gracefully. An optional second argument, a reviver function, can transform values as they're parsed (for example, turning date strings back into Date objects).
Syntax
const value = JSON.parse(text)
const value = JSON.parse(text, reviver)
JSON.parse('{"a":1}') // { a: 1 }
JSON.parse('[1, 2, 3]') // [1, 2, 3]
Parameters
The JSON.parse() method accepts the following parameters.
| Parameter | Description |
|---|---|
text |
The JSON string to parse. Must be valid JSON or it throws a SyntaxError. |
reviver |
Optional. A function called for each key/value to transform values as they are parsed. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const text = '{"name":"Ada","age":36}';
try {
const user = JSON.parse(text);
document.getElementById('out').textContent =
user.name + ' is ' + user.age; // Ada is 36
} catch (err) {
document.getElementById('out').textContent = 'Invalid JSON';
}
</script>
Best practices
- Always wrap
JSON.parse()intry/catch— invalid JSON throws aSyntaxError. - Pair it with JSON.stringify() to round-trip data through text storage.
- When using fetch(), prefer
await response.json(), which parses the body for you. - Use a reviver to restore special types, like turning ISO date strings back into
Dateobjects.
Frequently asked questions
What does JSON.parse() do?
Why does JSON.parse() throw an error?
try/catch to handle it.What is the difference between JSON.parse() and JSON.stringify()?
JSON.parse() turns a string into a value; JSON.stringify() turns a value into a string. They're inverses.Do I need JSON.parse() with fetch()?
await response.json() parses the response body for you. Use JSON.parse() when you already have a JSON string.