JSON.parse()
Parses a JSON string, constructing the JavaScript value or object described by the string.
JSON.parse(text, [reviver])This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Parses a JSON string, constructing the JavaScript value or object described by the string.
The `JSON.parse()` method is a static function that takes a JSON formatted string and converts it into a JavaScript value or object. This is a fundamental operation for working with data received from web servers (e.g., via AJAX requests), local storage, or file systems, as these sources often transmit data as JSON strings. The method can parse JSON representations of objects, arrays, strings, numbers, booleans, and `null`. If the input `text` is not a valid JSON string, `JSON.parse()` will throw a `SyntaxError`. The optional `reviver` argument is a function that can transform the parsed value before it is returned. It's called for each key-value pair in the parsed object (or array), allowing for custom deserialization logic, such as converting date strings back into `Date` objects. The time complexity is generally O(n) where n is the length of the JSON string, as it needs to iterate through the string to build the object.
Quick reference
Syntax
JSON.parse(text, [reviver])
Inputs
Parameters
See it in practice
Examples
Parsing a simple JSON object
const jsonString = '{"name": "Alice", "age": 30}';
const obj = JSON.parse(jsonString);
console.log(obj.name);
console.log(obj.age);Alice 30
The JSON string is converted into a JavaScript object, and its properties can be accessed.
Parsing a JSON array
const jsonArrayString = '[10, "hello", true]';
const arr = JSON.parse(jsonArrayString);
console.log(arr[0]);
console.log(arr[1]);10 hello
The JSON string representing an array is converted into a JavaScript array.
Using a reviver function
const jsonDateString = '{"event": "meeting", "date": "2023-10-26T10:00:00.000Z"}';
const parsedWithReviver = JSON.parse(jsonDateString, (key, value) => {
if (key === 'date') {
return new Date(value);
}
return value;
});
console.log(parsedWithReviver.date instanceof Date);
console.log(parsedWithReviver.date.getFullYear());true 2023
The `reviver` function intercepts the 'date' property and converts its string value into a `Date` object.
Debug faster
Common Errors
SyntaxError for invalid JSON
Cause: Attempting to parse a string that is not valid JSON (e.g., unquoted keys, trailing commas, single quotes instead of double quotes for strings).
Fix: Ensure the input string strictly adheres to the JSON specification. Use a JSON validator if unsure.
const invalidJson = "{name: 'Bob'}"; // Keys must be double-quoted, strings must be double-quoted
try {
JSON.parse(invalidJson);
} catch (e) {
console.error(e.name + ": " + e.message);
}Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Parses a JSON string, constructing the JavaScript value or object described by the string.
text: The JSON string to parse. reviver: An optional function that transforms the results.
SyntaxError for invalid JSON: Ensure the input string strictly adheres to the JSON specification. Use a JSON validator if unsure.