JSON.parse()

What you’ll learn
JSON is how browsers, servers, and tools exchange structured data as text. In JavaScript, JSON.parse() is the standard way to turn that text into values you can read and manipulate.
This guide covers the method signature, a plain example, the optional reviver, and how to catch SyntaxError when the input is not valid JSON.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, language-independent text format for data interchange. It is easy for humans to read and for machines to parse. The format is built on two structures:
- A collection of name/value pairs — in JavaScript this becomes an object (and similar types in other languages).
- An ordered list of values — in JavaScript this becomes an array.
Example JSON
Here is a single JSON object that mixes strings, numbers, booleans, arrays, and nested objects — the kind of payload you often receive from an HTTP API.
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"courses": ["Math", "Science", "History"],
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipcode": "12345"
}
}What JSON.parse() does
The JSON.parse() method parses a string as JSON and produces the corresponding JavaScript value. Use it when you have JSON text (for example from fetch().then(r => r.text()) or a file) and need an object or array in memory.
Syntax
JSON.parse(text[, reviver])text— The string to parse. It must be valid JSON.reviver(optional) — A function called for each key–value pair while the result is built; return the value you want in the final tree (orundefinedto drop a property).
Basic example
A JSON string becomes a JavaScript object; properties are accessed with dot or bracket notation.
const jsonString = '{"name": "Jane Doe", "age": 25, "isStudent": true}';
const jsonObject = JSON.parse(jsonString);
console.log(jsonObject.name); // "Jane Doe"
console.log(jsonObject.age); // 25
console.log(jsonObject.isStudent); // trueUsing a reviver function
The reviver runs during parsing so you can coerce types — here an ISO date string becomes a Date instance.
const jsonString = '{"name": "Jane Doe", "birthDate": "1990-01-01T00:00:00.000Z"}';
const jsonObject = JSON.parse(jsonString, (key, value) => {
if (key === "birthDate") {
return new Date(value);
}
return value;
});
console.log(jsonObject.birthDate instanceof Date); // true
console.log(jsonObject.birthDate.toISOString()); // "1990-01-01T00:00:00.000Z"Error handling
If the string is not valid JSON, JSON.parse throws SyntaxError. Wrap parsing in try/catch so a bad payload does not crash your app. The exact message text depends on the JavaScript engine.
const jsonString = '{"name": "John Doe", "age": 30'; // invalid: missing closing brace
try {
const jsonObject = JSON.parse(jsonString);
} catch (e) {
console.error("Parsing failed:", e.name, e.message);
}Untrusted input
Parsing succeeds only if the text is JSON; it does not replace validation. For data from users or third-party APIs, check required fields, types, and reasonable size before trusting the object — especially before spreading into state or passing to sensitive APIs.
Key takeaways
JSON.parse(text) converts a JSON string into a JavaScript object, array, or primitive.
The optional reviver lets you transform values while the tree is constructed (dates, enums, omitting keys).
Invalid JSON throws SyntaxError; use try/catch and validate payloads you did not author.
Frequently asked questions
null. It does not invent undefined for absent keys; those keys are simply missing on the object.SyntaxError.(key, value) => newValue callback invoked while the parsed structure is built. Common uses include parsing date strings into Date objects or filtering sensitive fields.eval runs arbitrary JavaScript. JSON.parse only accepts JSON text and is the correct, safe API for deserializing JSON.Next: JSON.stringify()
After you can read JSON, learn the paired API that writes JSON from JavaScript values.
JSON.parse is not the same as eval: it only accepts JSON text and returns plain objects, arrays, and primitives. You should still validate parsed data from untrusted sources (shape, types, size) before merging into application state.
10 people found this page helpful
