Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
possible duplicate of Serializing to JSON in jQuery – outis Dec 26 '11 at 10:10

7 Answers

up vote 341 down vote accepted

Most browsers support JSON.parse(), which is defined in ECMA-262 5th Edition (the specification that JS is based on). Its usage is simple:

var json = '{"result":true,"count":1}',
    obj = JSON.parse(json);

alert(obj.count);

For the browsers that don't you can implement it using json2.js.

As noted in the comments, if you're already using jQuery, there is a $.parseJSON function that maps to JSON.parse if available or a form of eval in older browsers. However, this performs additional, unnecessary checks that are also performed by JSON.parse, so for the best all round performance I'd recommend using it like so:

var json = '{"result":true,"count":1}',
    obj = JSON && JSON.parse(json) || $.parseJSON(json);

This will ensure you use native JSON.parse immediately, rather than having jQuery perform sanity checks on the string before passing it to the native parsing function.

share|improve this answer
i think JSON.parse not working in IE ?? – Marwan Nov 16 '11 at 9:26
7  
@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 '11 at 9:46
@AndyE, you can also use jQuery too (in case it already included jQuery, he doesn't need an extra library.) – Derek 朕會功夫 May 8 '12 at 23:25
2  
Unless he also needs JSON.stringify() – ThiefMaster May 9 '12 at 11:19
3  
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 at 9:42
show 1 more comment

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 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 older browser, you can always use the JSON.parse method. This is really the ideal solution for the future.

share|improve this answer
Great man! I couldn't import the JSON lib, because it conflicted with other libs – Tahir Malik Oct 1 '12 at 16:09

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);
    });
});
share|improve this answer

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

<script>
window.JSON || 
    document.write('<script src="//cdnjs.cloudflare.com/ajax/libs/json3/3.2.4/json3.min.js"><\/script>');
</script>

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

share|improve this answer

Without using a library you can use eval - the only time you should use. It's safer to use a library though.

eg...

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

var parsedJSON = eval('('+response+')');

var result=parsedJSON.result;
var count=parsedJSON.count;

alert('result:'+result+' count:'+count);
share|improve this answer
eval cant handle a json string which return as HTML – user192344 Mar 13 '12 at 1:34
7  
IF it's html-encoded it's not JSON anymore. – ThiefMaster May 9 '12 at 11:20
1  
eval expects valid javascript, which JSON might not be, so eval cannot parse some valid JSON texts (for example, U+2028 is valid in JSON, not valid in javascript). – Marc Lehmann Jul 4 at 1:31

I thought JSON.parse(myObject) would work. But depending on the browsers, it might be worth using eval('('+myObject+')'). The only issue I can recommend watching out for is the multi-level list in JSON.

share|improve this answer

You can either use eval function as above. (Dont forget the extra braces. You will know why when you dig deep) or simply use jQuery function parseJSON.

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

var parsedJSON = $.parseJSON(response);

Cheers!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.