Convert JSON string to JavaScript objects safely

February 03, 2014 | Posted by: Fredrik | Tags: Javascript, JSON, jQuery
If you have a JSON string that you want to convert into JavaScript object but doesn't want to use eval (which you should avoid due to the security implications), you can either use jQuery.parseJSON() or - if you're not using jQuery - use a dedicated JSON library such as JSON 3.
// using jQuery
var myObject = jQuery.parseJSON(myJSONString);

// using JSON 3 (for polyfill in older browsers)
var myObject2 = JSON.parse(myJSONString);

Comments

comments powered by Disqus