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

<p>Obviously I can do this unsafely with something like:</p>

<pre><code>var obj = eval("(" + json + ')');
</code></pre>

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

[{'answer_id': 45019, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 5, 'selected': False, 'text': '<p>I\'m not sure about other ways to do it but here\'s how you do it in <a href="http://www.prototypejs.org/learn/json" rel="nofollow noreferrer">Prototype (JSON tutorial)</a>.</p>\n\n<pre><code>new Ajax.Request(\'/some_url\', {\n  method:\'get\',\n  requestHeaders: {Accept: \'application/json\'},\n  onSuccess: function(transport){\n    var json = transport.responseText.evalJSON(true);\n  }\n});\n</code></pre>\n\n<p>Calling <code>evalJSON()</code> with true as the argument sanitizes the incoming string.</p>\n'}, {'answer_id': 45020, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 7, 'selected': False, 'text': '<p><em>This answer is for IE &lt; 7, for modern browsers check Jonathan\'s answer above.</em></p>\n\n<p><em>This answer is outdated and <a href="https://stackoverflow.com/a/5686237/542251">Jonathan\'s answer above</a> (<code>JSON.parse(jsonString)</code>) is now the best answer</em>.</p>\n\n<p><a href="http://json.org/" rel="noreferrer">JSON.org</a> has JSON parsers for many languages including four different ones for JavaScript.  I believe most people would consider <a href="http://www.json.org/js.html" rel="noreferrer">json2.js</a> their goto implementation.</p>\n'}, {'answer_id': 233630, 'author': 'Leanan', 'author_id': 22390, 'author_profile': 'https://Stackoverflow.com/users/22390', 'pm_score': 4, 'selected': False, 'text': '<p>If you\'re using <a href="https://jquery.com/" rel="noreferrer">jQuery</a>, you can also use:</p>\n\n<pre><code>$.getJSON(url, function(data) { });\n</code></pre>\n\n<p>Then you can do things like</p>\n\n<pre><code>data.key1.something\ndata.key1.something_else\n</code></pre>\n\n<p>etc.</p>\n'}, {'answer_id': 2778898, 'author': 'Prahlad', 'author_id': 190477, 'author_profile': 'https://Stackoverflow.com/users/190477', 'pm_score': 4, 'selected': False, 'text': "<pre><code>$.ajax({\n  url: url,\n  dataType: 'json',\n  data: data,\n  success: callback\n});\n</code></pre>\n\n<p>The callback is passed the returned data, which will be a JavaScript object or array as defined by the JSON structure and parsed using the <code>$.parseJSON()</code> method.</p>\n"}, {'answer_id': 3627901, 'author': 'Alex V', 'author_id': 327934, 'author_profile': 'https://Stackoverflow.com/users/327934', 'pm_score': 10, 'selected': False, 'text': '<p>The jQuery method is now deprecated. Use this method instead:</p>\n\n<pre><code>let jsonObject = JSON.parse(jsonString);\n</code></pre>\n\n<hr>\n\n<p><em>Original answer using deprecated jQuery functionality</em>:</p>\n\n<p>If you\'re using jQuery just use:</p>\n\n<pre><code>jQuery.parseJSON( jsonString );\n</code></pre>\n\n<p>It\'s exactly what you\'re looking for (see the jQuery <a href="http://api.jquery.com/jQuery.parseJSON/" rel="noreferrer">documentation</a>).</p>\n'}, {'answer_id': 5686237, 'author': 'Jonathan.', 'author_id': 191463, 'author_profile': 'https://Stackoverflow.com/users/191463', 'pm_score': 12, 'selected': True, 'text': '<p><a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" rel="noreferrer"><code>JSON.parse(jsonString)</code></a> is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.</p>\n'}, {'answer_id': 16273454, 'author': 'Cody', 'author_id': 1153121, 'author_profile': 'https://Stackoverflow.com/users/1153121', 'pm_score': 5, 'selected': False, 'text': '<p>This seems to be the issue:</p>\n\n<p>An input that is received via Ajax websocket etc, and it will be in String format,  but you need to know if it is <code>JSON.parsable</code>. The touble is, if you always run it through <code>JSON.parse</code>, the program MAY continue "successfully" but you\'ll still see an error thrown in the console with the dreaded <code>"Error: unexpected token \'x\'"</code>.</p>\n\n<pre><code>var data;\n\ntry {\n  data = JSON.parse(jqxhr.responseText);\n} catch (_error) {}\n\ndata || (data = {\n  message: \'Server error, please retry\'\n});\n</code></pre>\n'}, {'answer_id': 20601233, 'author': 'Ronald Coarite', 'author_id': 2154661, 'author_profile': 'https://Stackoverflow.com/users/2154661', 'pm_score': 6, 'selected': False, 'text': '<p>Use the simple code example in "<a href="http://msdn.microsoft.com/en-us/library/ie/cc836466%28v=vs.94%29.aspx" rel="noreferrer">JSON.parse()</a>":</p>\n\n<pre><code>var jsontext = \'{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}\';\nvar contact = JSON.parse(jsontext);\n</code></pre>\n\n<p>and reversing it:</p>\n\n<pre><code>var str = JSON.stringify(arr);\n</code></pre>\n'}, {'answer_id': 24765981, 'author': 'GPrathap', 'author_id': 1574779, 'author_profile': 'https://Stackoverflow.com/users/1574779', 'pm_score': 3, 'selected': False, 'text': "<p>Try using the method with this Data object.  ex:<code>Data='{result:true,count:1}</code>'</p>\n\n<pre><code>try {\n  eval('var obj=' + Data);\n  console.log(obj.count);\n}\ncatch(e) {\n  console.log(e.message);\n}\n</code></pre>\n\n<p>This method really helps in Nodejs when <strong>you are working with serial port programming</strong> </p>\n"}, {'answer_id': 26377600, 'author': 'lessisawesome', 'author_id': 2651695, 'author_profile': 'https://Stackoverflow.com/users/2651695', 'pm_score': 4, 'selected': False, 'text': "<p>Just for fun, here is a way using a function:</p>\n\n<pre><code> jsonObject = (new Function('return ' + jsonFormatData))()\n</code></pre>\n"}, {'answer_id': 28585014, 'author': 'Dorian', 'author_id': 407213, 'author_profile': 'https://Stackoverflow.com/users/407213', 'pm_score': 3, 'selected': False, 'text': '<p>I found a "better" way:</p>\n\n<p>In CoffeeScript:</p>\n\n<pre><code>try data = JSON.parse(jqxhr.responseText)\ndata ||= { message: \'Server error, please retry\' }\n</code></pre>\n\n<p>In Javascript:</p>\n\n<pre><code>var data;\n\ntry {\n  data = JSON.parse(jqxhr.responseText);\n} catch (_error) {}\n\ndata || (data = {\n  message: \'Server error, please retry\'\n});\n</code></pre>\n'}, {'answer_id': 29793524, 'author': 'Barath Kumar', 'author_id': 2920768, 'author_profile': 'https://Stackoverflow.com/users/2920768', 'pm_score': 4, 'selected': False, 'text': "<p>Using <code>JSON.parse</code> is probably the best way.</p>\n<p>Here's an example</p>\n<pre><code>var jsonRes = '{ &quot;students&quot; : [' +\n          '{ &quot;firstName&quot;:&quot;Michel&quot; , &quot;lastName&quot;:&quot;John&quot; ,&quot;age&quot;:18},' +\n          '{ &quot;firstName&quot;:&quot;Richard&quot; , &quot;lastName&quot;:&quot;Joe&quot;,&quot;age&quot;:20 },' +\n          '{ &quot;firstName&quot;:&quot;James&quot; , &quot;lastName&quot;:&quot;Henry&quot;,&quot;age&quot;:15 } ]}';\nvar studentObject = JSON.parse(jsonRes);\n</code></pre>\n"}, {'answer_id': 35517811, 'author': 'Jorgesys', 'author_id': 250260, 'author_profile': 'https://Stackoverflow.com/users/250260', 'pm_score': 3, 'selected': False, 'text': '<p>The easiest way using <code>parse()</code> method:</p>\n\n<pre><code>var response = \'{"result":true,"count":1}\';\nvar JsonObject= JSON.parse(response);\n</code></pre>\n\n<p>Then you can get the values of the JSON elements, for example:</p>\n\n<pre><code>var myResponseResult = JsonObject.result;\nvar myResponseCount = JsonObject.count;\n</code></pre>\n\n<p>Using <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a> as described in the <a href="http://api.jquery.com/jQuery.parseJSON/" rel="nofollow noreferrer"><code>jQuery.parseJSON()</code></a> documentation:</p>\n\n<pre><code>JSON.parse(jsonString);\n</code></pre>\n'}, {'answer_id': 40949422, 'author': 'Pushkar Kathuria', 'author_id': 2171599, 'author_profile': 'https://Stackoverflow.com/users/2171599', 'pm_score': 2, 'selected': False, 'text': '<p><code>JSON.parse()</code> converts any JSON string passed into the function into a JSON object.</p>\n\n<p>To understand it better, press <kbd>F12</kbd> to open "Inspect Element" in your browser and go to the console to write the following commands:</p>\n\n<pre><code>var response = \'{"result":true,"count":1}\'; //sample json object(string form)\nJSON.parse(response); //converts passed string to JSON Object.\n</code></pre>\n\n<p>Now run the command:</p>\n\n<pre><code>console.log(JSON.parse(response));\n</code></pre>\n\n<p>You\'ll get output as an Object <code>{result: true, count: 1}</code>.</p>\n\n<p>In order to use that Object, you can assign it to the variable, maybe <code>obj</code>:</p>\n\n<pre><code>var obj = JSON.parse(response);\n</code></pre>\n\n<p>By using <code>obj</code> and the dot (<code>.</code>) operator you can access properties of the JSON object.</p>\n\n<p>Try to run the command:</p>\n\n<pre><code>console.log(obj.result);\n</code></pre>\n'}, {'answer_id': 41223251, 'author': 'Shekhar Tyagi', 'author_id': 7250429, 'author_profile': 'https://Stackoverflow.com/users/7250429', 'pm_score': 2, 'selected': False, 'text': '<pre><code>JSON.parse(jsonString);\n</code></pre>\n\n<p>json.parse will change into object.</p>\n'}, {'answer_id': 42235893, 'author': 'Tahsin Turkoz', 'author_id': 3618397, 'author_profile': 'https://Stackoverflow.com/users/3618397', 'pm_score': 3, 'selected': False, 'text': "<p>JSON parsing is always a pain. If the input is not as expected it throws an error and crashes what you are doing. </p>\n\n<p>You can use the following tiny function to safely parse your input. It always turns an object even if the input is not valid or is already an object which is better for most cases:</p>\n\n<pre><code>JSON.safeParse = function (input, def) {\n  // Convert null to empty object\n  if (!input) {\n    return def || {};\n  } else if (Object.prototype.toString.call(input) === '[object Object]') {\n    return input;\n  }\n  try {\n    return JSON.parse(input);\n  } catch (e) {\n    return def || {};\n  }\n};\n</code></pre>\n"}, {'answer_id': 44635628, 'author': 'Liuver Reynier Durán Pérez', 'author_id': 6827370, 'author_profile': 'https://Stackoverflow.com/users/6827370', 'pm_score': 2, 'selected': False, 'text': '<p>Converting the object to JSON, and then parsing it, works for me, like:</p>\n\n<pre><code>JSON.parse(JSON.stringify(object))\n</code></pre>\n'}, {'answer_id': 45322136, 'author': 'Durgpal Singh', 'author_id': 1759015, 'author_profile': 'https://Stackoverflow.com/users/1759015', 'pm_score': 1, 'selected': False, 'text': '<p>You also can use <code>reviver</code> function to filter.</p>\n\n<pre><code>var data = JSON.parse(jsonString, function reviver(key, value) {\n   //your code here to filter\n});\n</code></pre>\n\n<p>For more information read <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" rel="nofollow noreferrer"><code>JSON.parse</code></a>.</p>\n'}, {'answer_id': 47897689, 'author': 'Salomon Zhang', 'author_id': 6031990, 'author_profile': 'https://Stackoverflow.com/users/6031990', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Official documentation</strong>:</p>\n\n<p>The <code>JSON.parse()</code> method parses a JSON string, constructing the JavaScript value or object described by the string. An optional <code>reviver</code> function can be provided to perform a transformation on the resulting object before it is returned.</p>\n\n<p><strong>Syntax:</strong></p>\n\n<pre><code>JSON.parse(text[, reviver])\n</code></pre>\n\n<p><strong>Parameters:</strong></p>\n\n<p><code>text</code>\n: The string to parse as JSON. See the JSON object for a description of JSON syntax.</p>\n\n<p><code>reviver (optional)</code>\n: If a function, this prescribes how the value originally produced by parsing is transformed, before being returned.</p>\n\n<p><strong>Return value</strong></p>\n\n<p>The Object corresponding to the given JSON text.</p>\n\n<p><strong>Exceptions</strong></p>\n\n<p>Throws a SyntaxError exception if the string to parse is not valid JSON.</p>\n'}, {'answer_id': 48858463, 'author': 'Codebeat', 'author_id': 565244, 'author_profile': 'https://Stackoverflow.com/users/565244', 'pm_score': 1, 'selected': False, 'text': '<p>Older question, I know, however nobody notice this solution by using <code>new Function()</code>, an anonymous function that returns the data. </p>\n\n<hr>\n\n<p>Just an example:</p>\n\n<pre><code> var oData = \'test1:"This is my object",test2:"This is my object"\';\n\n if( typeof oData !== \'object\' )\n  try {\n   oData = (new Function(\'return {\'+oData+\'};\'))();\n  }\n  catch(e) { oData=false; }\n\n if( typeof oData !== \'object\' )\n  { alert( \'Error in code\' ); }\n else {\n        alert( oData.test1 );\n        alert( oData.test2 );\n      }\n</code></pre>\n\n<hr>\n\n<p>This is a little more safe because it executes inside a function and do not compile in your code directly. So if there is a function declaration inside it, it will not be bound to the default window object. </p>\n\n<p>I use this to \'compile\' configuration settings of DOM elements (for example the data attribute) simple and fast.  </p>\n'}, {'answer_id': 50597756, 'author': 'Supun Dharmarathne', 'author_id': 1644446, 'author_profile': 'https://Stackoverflow.com/users/1644446', 'pm_score': -1, 'selected': False, 'text': '<p>Try this. This one is written in typescript.</p>\n<pre><code>export function safeJsonParse(str: string) {\n    try {\n        return JSON.parse(str);\n    } catch (e) {\n        return str;\n    }\n}\n</code></pre>\n'}, {'answer_id': 51814506, 'author': 'Amitesh Bharti', 'author_id': 2745436, 'author_profile': 'https://Stackoverflow.com/users/2745436', 'pm_score': 3, 'selected': False, 'text': '<p>Parse the JSON string with <code>JSON.parse()</code>, and the data becomes a JavaScript object:</p>\n\n<pre><code>JSON.parse(jsonString)\n</code></pre>\n\n<p>Here, JSON represents to process JSON dataset.</p>\n\n<p>Imagine we received this text from a web server:</p>\n\n<pre><code>\'{ "name":"John", "age":30, "city":"New York"}\'\n</code></pre>\n\n<p>To parse into a JSON object:</p>\n\n<pre><code>var obj = JSON.parse(\'{ "name":"John", "age":30, "city":"New York"}\'); \n</code></pre>\n\n<p>Here <code>obj</code> is the respective JSON object which looks like:</p>\n\n<pre><code>{ "name":"John", "age":30, "city":"New York"}\n</code></pre>\n\n<p>To fetch a value use the <code>.</code> operator:</p>\n\n<pre><code>obj.name // John\nobj.age //30\n</code></pre>\n\n<p>Convert a JavaScript object into a string with <code>JSON.stringify()</code>.</p>\n'}, {'answer_id': 51907405, 'author': 'Willem van der Veen', 'author_id': 8059459, 'author_profile': 'https://Stackoverflow.com/users/8059459', 'pm_score': 1, 'selected': False, 'text': '<h2>Summary:</h2>\n\n<p>Javascript (both browser and NodeJS) have a built in <code>JSON</code> object. On this Object are 2 convenient methods for dealing with <code>JSON</code>. They are the following:</p>\n\n<ol>\n<li><code>JSON.parse()</code>      Takes <code>JSON</code> as argument, returns JS object</li>\n<li><code>JSON.stringify()</code>  Takes JS object as argument returns <code>JSON</code> object</li>\n</ol>\n\n<h2>Other applications:</h2>\n\n<p>Besides for very conveniently dealing with <code>JSON</code> they have can be used for other means.  The combination of both <code>JSON</code> methods allows us to make very easy make deep clones of arrays or objects. For example:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>let arr1 = [1, 2, [3 ,4]];\r\nlet newArr = arr1.slice();\r\n\r\narr1[2][0] = \'changed\'; \r\nconsole.log(newArr); // not a deep clone\r\n\r\nlet arr2 = [1, 2, [3 ,4]];\r\nlet newArrDeepclone = JSON.parse(JSON.stringify(arr2));\r\n\r\narr2[2][0] = \'changed\'; \r\nconsole.log(newArrDeepclone); // A deep clone, values unchanged</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 55178226, 'author': 'Hamid Araghi', 'author_id': 4411896, 'author_profile': 'https://Stackoverflow.com/users/4411896', 'pm_score': 2, 'selected': False, 'text': '<p>If we have a string like this:</p>\n\n<pre><code>"{\\"status\\":1,\\"token\\":\\"65b4352b2dfc4957a09add0ce5714059\\"}"\n</code></pre>\n\n<p>then we can simply use <code>JSON.parse</code> twice to convert this string to a JSON object:</p>\n\n<pre><code>var sampleString = "{\\"status\\":1,\\"token\\":\\"65b4352b2dfc4957a09add0ce5714059\\"}"\nvar jsonString= JSON.parse(sampleString)\nvar jsonObject= JSON.parse(jsonString)\n</code></pre>\n\n<p>And we can extract values from the JSON object using:</p>\n\n<pre><code>// instead of last JSON.parse:\nvar { status, token } = JSON.parse(jsonString);\n</code></pre>\n\n<p>The result will be:</p>\n\n<pre><code>status = 1 and token = 65b4352b2dfc4957a09add0ce5714059\n</code></pre>\n'}, {'answer_id': 60359491, 'author': 'MOnkey', 'author_id': 7414166, 'author_profile': 'https://Stackoverflow.com/users/7414166', 'pm_score': 1, 'selected': False, 'text': '<p>Just to the cover parse for different input types</p>\n\n<p>Parse the data with JSON.parse(), and the data becomes a JavaScript object.</p>\n\n<pre><code>var obj = JSON.parse(\'{ "name":"John", "age":30, "city":"New York"}\');\n</code></pre>\n\n<p>When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.</p>\n\n<pre><code>var myArr = JSON.parse(this.responseText);\nconsole.log(myArr[0]);\n</code></pre>\n\n<p>Date objects are not allowed in JSON.\nFor Dates do somthing like this</p>\n\n<pre><code>var text = \'{ "name":"John", "birth":"1986-12-14", "city":"New York"}\';\nvar obj = JSON.parse(text);\nobj.birth = new Date(obj.birth);\n</code></pre>\n\n<p>Functions are not allowed in JSON.\nIf you need to include a function, write it as a string.</p>\n\n<pre><code>var text = \'{ "name":"John", "age":"function () {return 30;}", "city":"New York"}\';\nvar obj = JSON.parse(text);\nobj.age = eval("(" + obj.age + ")");\n</code></pre>\n'}, {'answer_id': 63989306, 'author': 'Kamil Kiełczewski', 'author_id': 860099, 'author_profile': 'https://Stackoverflow.com/users/860099', 'pm_score': 2, 'selected': False, 'text': '<h1>Performance</h1>\n<p>There are already good answer for this question, but I was curious about performance and today 2020.09.21 I conduct tests on MacOs HighSierra 10.13.6 on Chrome v85, Safari v13.1.2 and Firefox v80 for chosen solutions.</p>\n<h2>Results</h2>\n<ul>\n<li><code>eval/Function</code> (A,B,C) approach is fast on Chrome (but for big-deep object N=1000 they crash: &quot;maximum stack call exceed)</li>\n<li><code>eval</code> (A) is fast/medium fast on all browsers</li>\n<li><code>JSON.parse</code> (D,E) are fastest on Safari and Firefox</li>\n</ul>\n<p><a href="https://i.stack.imgur.com/Wzn1w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wzn1w.png" alt="enter image description here" /></a></p>\n<h2>Details</h2>\n<p>I perform 4 tests cases:</p>\n<ul>\n<li>for small shallow object <a href="https://jsbench.me/ipkf9kvabu/1" rel="nofollow noreferrer">HERE</a></li>\n<li>for small deep object <a href="https://jsbench.me/57kf9ldcel/1" rel="nofollow noreferrer">HERE</a></li>\n<li>for big shallow object <a href="https://jsbench.me/5lkf9l9slg/1" rel="nofollow noreferrer">HERE</a></li>\n<li>for big deep object <a href="https://jsbench.me/s6kf9lghxa/1" rel="nofollow noreferrer">HERE</a></li>\n</ul>\n<p>Object used in above tests came from <a href="https://stackoverflow.com/a/61523278/860099">HERE</a></p>\n<p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">\r\n<div class="snippet-code snippet-currently-hidden">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>let obj_ShallowSmall = {\n  field0: false,\n  field1: true,\n  field2: 1,\n  field3: 0,\n  field4: null,\n  field5: [],\n  field6: {},\n  field7: "text7",\n  field8: "text8",\n}\n\nlet obj_DeepSmall = {\n  level0: {\n   level1: {\n    level2: {\n     level3: {\n      level4: {\n       level5: {\n        level6: {\n         level7: {\n          level8: {\n           level9: [[[[[[[[[[\'abc\']]]]]]]]]],\n  }}}}}}}}},\n};\n\nlet obj_ShallowBig = Array(1000).fill(0).reduce((a,c,i) =&gt; (a[\'field\'+i]=getField(i),a) ,{});\n\n\nlet obj_DeepBig = genDeepObject(1000);\n\n\n\n// ------------------\n// Show objects\n// ------------------\n\nconsole.log(\'obj_ShallowSmall:\',JSON.stringify(obj_ShallowSmall));\nconsole.log(\'obj_DeepSmall:\',JSON.stringify(obj_DeepSmall));\nconsole.log(\'obj_ShallowBig:\',JSON.stringify(obj_ShallowBig));\nconsole.log(\'obj_DeepBig:\',JSON.stringify(obj_DeepBig));\n\n\n\n\n// ------------------\n// HELPERS\n// ------------------\n\nfunction getField(k) {\n  let i=k%10;\n  if(i==0) return false;\n  if(i==1) return true;\n  if(i==2) return k;\n  if(i==3) return 0;\n  if(i==4) return null;\n  if(i==5) return [];\n  if(i==6) return {};  \n  if(i&gt;=7) return "text"+k;\n}\n\nfunction genDeepObject(N) {\n  // generate: {level0:{level1:{...levelN: {end:[[[...N-times...[\'abc\']...]]] }}}...}}}\n  let obj={};\n  let o=obj;\n  let arr = [];\n  let a=arr;\n\n  for(let i=0; i&lt;N; i++) {\n    o[\'level\'+i]={};\n    o=o[\'level\'+i];\n    let aa=[];\n    a.push(aa);\n    a=aa;\n  }\n\n  a[0]=\'abc\';\n  o[\'end\']=arr;\n  return obj;\n}</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>Below snippet presents chosen solutions</p>\n<p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">\r\n<div class="snippet-code snippet-currently-hidden">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>// src: https://stackoverflow.com/q/45015/860099\nfunction A(json) {\n  return eval("(" + json + \')\');\n}\n\n// https://stackoverflow.com/a/26377600/860099\nfunction B(json) {\n  return (new Function(\'return (\'+json+\')\'))()\n}\n\n\n// improved https://stackoverflow.com/a/26377600/860099\nfunction C(json) {\n  return Function(\'return (\'+json+\')\')()\n}\n\n// src: https://stackoverflow.com/a/5686237/860099\nfunction D(json) {\n  return JSON.parse(json);\n}\n\n// src: https://stackoverflow.com/a/233630/860099\nfunction E(json) {\n  return $.parseJSON(json)\n}\n\n\n\n \n// --------------------\n// TEST\n// --------------------\n\nlet json = \'{"a":"abc","b":"123","d":[1,2,3],"e":{"a":1,"b":2,"c":3}}\';\n\n[A,B,C,D,E].map(f=&gt; {  \n  console.log(\n    f.name + \' \' + JSON.stringify(f(json))\n  )})</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;\nThis shippet only presents functions used in performance tests - it not perform tests itself!</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>And here are example results for chrome</p>\n<p><a href="https://i.stack.imgur.com/mzfaY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mzfaY.png" alt="enter image description here" /></a></p>\n'}, {'answer_id': 73305592, 'author': 'tebe.app', 'author_id': 17773570, 'author_profile': 'https://Stackoverflow.com/users/17773570', 'pm_score': 0, 'selected': False, 'text': '<p>Another option</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const json = \'{ "fruit": "pineapple", "fingers": 10 }\'\nlet j0s,j1s,j2s,j3s\nconsole.log(`{ "${j0s="fruit"}": "${j1s="pineapple"}", "${j2s="fingers"}": ${j3s="10"} }`)</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 73799088, 'author': 'S M Samnoon Abrar', 'author_id': 8188682, 'author_profile': 'https://Stackoverflow.com/users/8188682', 'pm_score': 2, 'selected': False, 'text': '<p><strong>The recommended approach to parse JSON in JavaScript is to use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse" rel="nofollow noreferrer"><code>JSON.parse()</code></a></strong></p>\n<h2>Background</h2>\n<p>The <a href="https://jsonapi.org/" rel="nofollow noreferrer"><code>JSON</code></a> API was introduced with <a href="https://en.wikipedia.org/wiki/ECMAScript#5th_Edition" rel="nofollow noreferrer">ECMAScript 5</a> and has since been implemented in &gt;99% of browsers by market share.</p>\n<p>jQuery once had a <a href="https://api.jquery.com/jquery.parsejson/" rel="nofollow noreferrer"><code>$.parseJSON()</code></a> function, but it was deprecated with jQuery 3.0. In any case, for a long time, it was nothing more than a wrapper around <code>JSON.parse()</code>.</p>\n<h2>Example</h2>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const json = \'{ "city": "Boston", "population": 500000 }\';\nconst object = JSON.parse(json);\nconsole.log(object.city, object.population);</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<hr />\n<h2>Browser Compatibility</h2>\n<p><em><strong>Is JSON.parse supported by all major browsers?</strong></em></p>\n<p>Pretty much, <strong>yes</strong> (see <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#browser_compatibility" rel="nofollow noreferrer">reference</a>).</p>\n'}]
['https://Stackoverflow.com/questions/45015', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']