Reverse of JSON.stringify?

thelolcat picture thelolcat · Jun 23, 2012 · Viewed 269.8k times · Source

I'm stringyfing an object like {'foo': 'bar'}

How can I turn the string back to an object?

Answer

Chase Florell picture Chase Florell · Jun 23, 2012

You need to JSON.parse() the string.

var str = '{"hello":"world"}';
try {
  var obj = JSON.parse(str); // this is how you parse a string into JSON 
  document.body.innerHTML += obj.hello;
} catch (ex) {
  console.error(ex);
}