HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

Safely turning a JSON string into an object

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
safelyobjectturningstringintojson

Problem

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

Obviously I can do this unsafely with something like:

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


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

Solution

JSON.parse(jsonString) is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.

Context

Stack Overflow Q#45015, score: 2127

Revisions (0)

No revisions yet.