Parse JSON in JavaScript?

ID : 583

viewed : 198

Tags : javascriptjsonparsing

Top 5 Answer for Parse JSON in JavaScript?

vote vote


The standard way to parse JSON in JavaScript is JSON.parse()

The JSON API was introduced with ES5 (2011) and has since been implemented in >99% of browsers by market share, and Node.js. Its usage is simple:

const json = '{ "fruit": "pineapple", "fingers": 10 }'; const obj = JSON.parse(json); console.log(obj.fruit, obj.fingers);

The only time you won't be able to use JSON.parse() is if you are programming for an ancient browser, such as IE 7 (2006), IE 6 (2001), Firefox 3 (2008), Safari 3.x (2009), etc. Alternatively, you may be in an esoteric JavaScript environment that doesn't include the standard APIs. In these cases, use json2.js, the reference implementation of JSON written by Douglas Crockford, the inventor of JSON. That library will provide an implementation of JSON.parse().

When processing extremely large JSON files, JSON.parse() may choke because of its synchronous nature and design. To resolve this, the JSON website recommends third-party libraries such as Oboe.js and clarinet, which provide streaming JSON parsing.

jQuery once had a $.parseJSON() function, but it was deprecated with jQuery 3.0. In any case, for a long time, it was nothing more than a wrapper around JSON.parse().

vote vote



This answer stems from an ancient era of JavaScript programming during which there was no builtin way to parse JSON. The advice given here is no longer applicable and probably dangerous. From a modern perspective, parsing JSON by involving jQuery or calling eval() is nonsense. Unless you need to support IE 7 or Firefox 3.0, the correct way to parse JSON is JSON.parse().

First of all, you have to make sure that the JSON code is valid.

After that, I would recommend using a JavaScript library such as jQuery or Prototype if you can because these things are handled well in those libraries.

On the other hand, if you don't want to use a library and you can vouch for the validity of the JSON object, I would simply wrap the string in an anonymous function and use the eval function.

This is not recommended if you are getting the JSON object from another source that isn't absolutely trusted because the eval function allows for renegade code if you will.

Here is an example of using the eval function:

var strJSON = '{"result":true,"count":1}'; var objJSON = eval("(function(){return " + strJSON + ";})()"); alert(objJSON.result); alert(objJSON.count); 

If you control what browser is being used or you are not worried people with an older browser, you can always use the JSON.parse method.

This is really the ideal solution for the future.

vote vote


If you are getting this from an outside site it might be helpful to use jQuery's getJSON. If it's a list you can iterate through it with $.each

$.getJSON(url, function (json) {     alert(json.result);     $.each(json.list, function (i, fb) {         alert(fb.result);     }); }); 
vote vote


If you want to use JSON 3 for older browsers, you can load it conditionally with:

<script>     window.JSON ||      document.write('<script src="//"><\/scr'+'ipt>'); </script> 

Now the standard window.JSON object is available to you no matter what browser a client is running.

vote vote


The following example will make it clear:

let contactJSON = '{"name":"John Doe","age":"11"}'; let contact = JSON.parse(contactJSON); console.log( + ", " + contact.age);  // Output: John Doe, 11 

Top 3 video Explaining Parse JSON in JavaScript?