Categories
javascript json parsing

Parse JSON in JavaScript? [duplicate]

1743

I want to parse a JSON string in JavaScript. The response is something like

var response="{"result":true,"count":1}";

How can I get the values result and count from this?

1

  • 2

    var json = ‘{“result”:true,”count”:1}’, obj = JSON.parse(json); console.log(obj.count); // if use in nodejs then use console

    Dec 19, 2016 at 13:06

2031

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().

13

  • 36

    @Marwan: IE 8+ supports JSON.parse(). For IE 6, 7 and other older browsers, you can use the json2.js I linked to from my post. Alternatively, but less securely, you can use eval.

    – Andy E

    Nov 16, 2011 at 9:46

  • 10

    Unless he also needs JSON.stringify()

    May 9, 2012 at 11:19

  • 17

    Note to reviewers: please thoroughly check peer edits before allowing them, as your actions may cause unwanted side-effects for users copying and pasting code from answers.

    – Andy E

    Apr 2, 2013 at 9:42

  • 12

    It’s not necessary to check for native support first and then fall back to jQuery. jQuery 1.10 tries JSON.parse first, then the own implementation. jQuery 2.x is directly calling JSON.parse without checking or fallback.

    – Jasha

    Nov 4, 2013 at 15:29


  • 4

    Browser support details: Can I use JSON parsing

    Jan 7, 2014 at 10:11

106

WARNING!

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.

5

  • Great man! I couldn’t import the JSON lib, because it conflicted with other libs

    Oct 1, 2012 at 16:09

  • 28

    eval() is OK to fulfill a job, while it may compile and execute any Javascript program, so there can be security issues. I think JSON.parse() is a better choice.

    – ray6080

    Sep 10, 2013 at 11:58

  • 4

    Note for passerby: here’s a good online tool to check if your JSON string is valid: jsonlint.com

    Jul 30, 2014 at 15:40

  • 17

    NO, NO, NO!!! Using eval to evaluate JSON is a really dangerous idea. Are you 100% certain there is no possibility that someone could inject their own code into your string?

    – Charles

    Jul 21, 2015 at 18:07

  • 4

    Worth mentioning that there’s no such a thing as a JSON object.

    Sep 2, 2016 at 15:29

63

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);
    });
});