Back to results for “Error Handling”
JavaScript › JSON & Data Serialization

How do you convert a JSON string back into a JavaScript object?

Beginner

Answer

JSON.parse() parses a JSON-formatted string and returns the corresponding JavaScript value (object, array, string, number, etc.); it throws a SyntaxError if the string isn't valid JSON.
const json = '{"name":"Sam","age":30}';
const obj = JSON.parse(json);
console.log(obj.name); // 'Sam'
Real-world example Parsing a JSON response body received from an API into a usable JavaScript object.

Common follow-ups: How do you safely handle a JSON.parse() call that might fail on malformed input?