JSON.parse() in JavaScript: The Complete Guide
JSON.parse() is the essential JavaScript method for converting JSON strings into JavaScript objects. Whether you're fetching data from APIs, reading configuration files, or working with localStorage, understanding JSON.parse() is fundamental to modern JavaScript development. This guide covers everything from basic usage to advanced techniques and common pitfalls.
Syntax
The syntax for JSON.parse() is straightforward:
JSON.parse(text);
JSON.parse(text, reviver);
- text: The JSON string you wish to parse.
- reviver (Optional): A function to customize the parsing process.
Handling Different Data Types
JSON.parse() is designed to handle JSON-formatted strings. Any deviation from the JSON format will result in a SyntaxError.
try {
JSON.parse("undefined");
} catch (e) {
console.error(e.message); // Unexpected token u in JSON at position 0
}
The Reviver Parameter
The reviver function is a powerful tool to filter or transform the parsed data.
let reviver = function(key, value) {
if (key === "birthday") {
return new Date(value);
}
return value;
};
let jsonString = '{"name":"John", "birthday":"1990-01-01T00:00:00.000Z"}';
let obj = JSON.parse(jsonString, reviver);
console.log(obj.birthday.getFullYear()); // 1990
Handling Exceptions
JSON.parse() will throw a SyntaxError if the string to be parsed is not valid JSON.
try {
JSON.parse("text");
} catch (e) {
console.error(e.message); // Unexpected token t in JSON at position 0
}
Practical Examples
Various practical examples illustrating the diverse use cases of JSON.parse().
// Basic Usage
let jsonString = '{"name":"John", "age":30, "city":"New York"}';
let obj = JSON.parse(jsonString);
console.log(obj.name); // John
// Using Reviver Function
let reviver = function(key, value) {
if (key === "age") {
return value + 1;
}
return value;
};
let newObj = JSON.parse(jsonString, reviver);
console.log(newObj.age); // 31