Advertisement
Beginner JavaScript, JSON & Data Serialization

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

Short 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.

Save JavaScript mock interview
Advertisement
AModel 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.
Likely follow-ups
  • How do you safely handle a JSON.parse() call that might fail on malformed input?
Related topics