2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Safely turning a JSON string into an object

Given a string of JSON data, how can you safely turn that string into a JavaScript object?

Obviously you can do this unsafely with something like...

var obj = eval("(" + json + ")");

...but that leaves us vulnerable to the json string containing other code, which it seems very dangerous to simply eval.


userimage

JSON.parse(jsonString);


Is a pure JavaScript approach so long as you can require a reasonably modern browser.

See alsohttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Answer is