In JavaScript, JSON.parse() reads a JSON string and turns it into a normal value you can use in code: an object, an array, a string, a number, a boolean, or null.
You use it after you get text from an API, a file, or localStorage. If the text is not valid JSON, JSON.parse() throws a SyntaxError. You can pass an optional reviver function to change values while they are being built.
Syntax
Call JSON.parse with the text. Add a reviver only when you need to change values.
JavaScript
JSON.parse(text);
JSON.parse(text, reviver);
Call
What it does
JSON.parse(text)
Reads the JSON string and returns the matching JavaScript value.
JSON.parse(text, reviver)
Same as above, then runs reviver(key, value) for each property so you can change or drop values.
Pair with JSON.stringify() when you need to go the other way: value to string.
Library declarations
JavaScript
// ECMAScript 5 (ES5)
JSON.parse(text)
JSON.parse(text, reviver)
// text : a string of JSON text
// reviver : optional function (key, value) => any
// returns : object | array | string | number | boolean | null
// throws : SyntaxError when text is not valid JSON
Parameters
Parameter
Description
text
Required. The JSON string to read. Keys and strings must use double quotes.
reviver
Optional. A function (key, value). Return the new value, or return undefined to remove that property.
Return Value
Return Value
Description
JavaScript value
An object, array, string, number, boolean, or null that matches the JSON text.
SyntaxError (thrown)
Thrown when the text is not valid JSON. Catch it with try...catch.
JavaScript Examples
Example 1 — Basic JSON.parse()
Turn a JSON string into an object and read a property.
JavaScript
const text = '{"name":"Ann","age":20}';
const user = JSON.parse(text);
console.log(user.name);
console.log(user.age);
console.log(typeof user);
The reviver runs for every key, including a final call with an empty key for the root value.
When key is "when", return a Date instead of the string.
Other values pass through unchanged with return value.
Example 3 — Catch Bad JSON
Invalid JSON throws. Wrap the call in try...catch.
JavaScript
const bad = "{name:'Ann'}"; // single quotes are not JSON
try {
const user = JSON.parse(bad);
console.log(user.name);
} catch (err) {
console.log("Error type: " + err.name);
console.log("Could not parse the text.");
}
const good = '{"name":"Ann"}';
const user = JSON.parse(good);
console.log("Parsed name: " + user.name);
Error type: SyntaxError
Could not parse the text.
Parsed name: Ann
How It Works
The first string uses single quotes, so it is not valid JSON.
catch runs and prints the error name.
The second string uses double quotes and parses cleanly.
Compatibility
Browser & Runtime Support
JSON.parse() is part of ES5. Every current browser and Node.js has it. No import is needed.
✓ ES5 · Widely supported
JSON.parse()
Supported in Chrome, Edge, Firefox, Safari, Opera, and Node.js for many years. Safe for everyday web and server code.
ES5Standard since
Google ChromeAll modern · Desktop & Mobile
Full support
Microsoft EdgeAll modern · Chromium and legacy
Full support
Mozilla FirefoxAll modern · Desktop & Mobile
Full support
Apple SafariAll modern · macOS & iOS
Full support
OperaAll modern · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebViewSupported · Chromium based
Supported
Node.js0.10+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerIE 8+ · Legacy
Supported
JSON.parse()Excellent
Bottom line: Safe to use everywhere. Prefer JSON.parse() over eval() when you need to read JSON text.
Frequently Asked Questions
It reads a JSON string and builds the matching JavaScript value: an object, array, string, number, boolean, or null.
text is the JSON string to read. reviver is optional. It is a function that runs for each key and value so you can change or drop values while parsing.
The JavaScript value that the JSON text describes. Bad JSON does not return a value. It throws a SyntaxError instead.
The text is not valid JSON. Common mistakes are single quotes, trailing commas, comments, or undefined. JSON needs double quotes around keys and strings.
Pass a reviver. Check the key or the string shape, then return new Date(value) when it looks like a date.
No. JSON.parse() only accepts JSON. It does not run code. Prefer JSON.parse() for data from APIs and files.