By user605334

2011-02-08 16:34:03 8 Comments

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?


@Andy E 2011-02-08 16:38:35

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

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


/* or ES6 */

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

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.

@Marwan 2011-11-16 09:26:04

i think JSON.parse not working in IE ??

@Andy E 2011-11-16 09:46:03

@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.

@Derek 朕會功夫 2012-05-08 23:25:16

@AndyE, you can also use jQuery too (in case it already included jQuery, he doesn't need an extra library.)

@ThiefMaster 2012-05-09 11:19:39

Unless he also needs JSON.stringify()

@Andy E 2013-04-02 09:42:59

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.

@chaz 2013-08-05 14:47:15

Shouldn't you just stick with $.parseJSON?

@Liam 2013-10-22 09:34:11

If you implement the JSON3 library this will fill in the gaps in browsers that do not natively support JavaScript, such as IE7

@Jasha 2013-11-04 15:29:01

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.

@Peter V. Mørch 2014-01-07 10:11:23

Browser support details: Can I use JSON parsing

@Janaka R Rajapaksha 2014-05-20 16:07:07

eval() will also parse json to javascript objects. But eval() function can compile and execute any javascripts too. So, if uses eval(), there is a potential security issue.

@hacks4life 2014-06-19 13:55:32

@AndyE, what if the JSON is '{"1":{"result":"true","count":"1"},"2":{"result":"false","c‌​ount":"2"}}' instead ? We'll have a [Object object] in output. How to do it in this case ?

@Barry Guvenkaya 2015-10-01 02:47:50

Since this post is one of the most popular ones on JSON in SO, can someone please include a thorough guideline to give an understanding on JSON escape characters as well?

@Joe Enos 2016-10-14 22:36:53

I'm going to be that guy who posts on a 5-year-old answer...If you execute obj = JSON && JSON.parse(json) || $.parseJSON(json), and the json string represents a falsy value, like 0 or null, then both JSON.parse and $.parseJSON will fire, because the JSON && JSON.parse(json) part evaluates to a falsy value. So it's technically not quite the best all around performance, but it would of course be a negligible difference.

@huwiler 2013-04-19 18:52:02

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

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

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

@Peter V. Mørch 2014-01-07 10:14:53

It is available to you after json3.min.js has finished loading. This doesn't give you a callback when it is available. So your code may work today, but won't work on wednesday when is suddenly slower than usual or the network is loaded or one of 10000 other reasons. RequireJS instead.

@huwiler 2014-01-29 03:18:41

Peter, that is not correct. Both the loading of external scripts and document.write are synchronous activities, so all scripts placed after will wait until it's loaded before executing. For loading just JSON3, this is a fine approach. RequireJS would come in handy if your project grew in complexity and had to load scripts with complex dependency relationships. Just remember that document.write will block page rendering, so place it at the bottom of your markup.

@Peter V. Mørch 2014-01-30 04:04:01

Sorry; I think you're right. Please disregard my comment as bogus.

@tomByrer 2014-03-10 01:43:11

Peter, your 1st comment is informative & useful (good to have that warning), just not applicable in 100% of cases. For a slightly more stable & faster CDN, you can use jsDelivr: //

@milestyle 2012-07-18 21:57:41

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) {
    $.each(json.list, function (i, fb) {

@user2359695 2014-07-01 17:51:06

As mentioned by numerous others, most browsers support JSON.parse and JSON.stringify.

Now, I'd also like to add that if you are using AngularJS (which I highly recommend), then it also provides the functionality that you require:

var myJson = '{"result": true, "count": 1}';
var obj = angular.fromJson(myJson);//equivalent to JSON.parse(myJson)
var backToJson = angular.toJson(obj);//equivalent to JSON.stringify(obj)

I just wanted to add the stuff about AngularJS to provide another option. NOTE that AngularJS doesn't officially support Internet Explorer 8 (and older versions, for that matter), though through experience most of the stuff seems to work pretty well.

@Brendon-Van-Heyzen 2014-07-17 20:09:10

If you use Dojo Toolkit:

require(["dojo/json"], function(JSON){
    JSON.parse('{"hello":"world"}', true);

@GGSoft 2018-01-02 20:05:16

var j='[{ "name":"John", "age":30, "city":"New York"}, { "name":"George", "age":48, "city":"Kutaisi"}]'; var obj = JSON.parse(j); alert(obj.length); for(var i=0; i<obj.length; i++){ alert(obj[i].city + ' ' + obj[i].age); }

@yassine 2014-09-24 18:08:50

An easy way to do it:

var data = '{"result":true,"count":1}';
var json = eval("[" +data+ "]")[0]; // ;)

@Jenna Leaf 2014-10-22 15:06:10

If you pass a string variable (a well-formed JSON string) to JSON.parse from MVC @Viewbag that has doublequote, '"', as quotes, you need to process it before JSON.parse (jsonstring)

    var jsonstring = '@ViewBag.jsonstring';
    jsonstring = jsonstring.replace(/&quot;/g, '"');  

@kay 2014-10-22 16:32:36

What do you mean with that? Why do you post an answer for an ancient question?

@Jenna Leaf 2014-10-22 16:40:16

What they said in the previous answers do not help the case that if the parameter value has double quotes in the string. It needs to be replaced globally with a real double quote !! I find out in a hard way just to share in case somebody has the same problem

@Jenna Leaf 2014-10-22 17:18:02

Kay: I clarified my posting right now, this is the first time I've ever tried to help. Please look at the posting again. You know that quote thing output from server page is a real problem to JSON.parse().

@Pushkar Kathuria 2015-02-18 10:08:41

JSON.parse() converts any JSON String passed into the function, to a JSON object.

For better understanding, press F12 to open the Inspect Element of your browser, and go to the console to write the following commands:

var response = '{"result":true,"count":1}'; // Sample JSON object (string form)
JSON.parse(response); // Converts passed string to a JSON object.

Now run the command:


You'll get output as Object {result: true, count: 1}.

In order to use that object, you can assign it to the variable, let's say obj:

var obj = JSON.parse(response);

Now by using obj and the dot(.) operator you can access properties of the JSON Object.

Try to run the command


@user5676965418 2015-05-15 05:42:47

If you like

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

you can access the JSON elements by JsonObject with (.) dot:


@Clarence Fredericks 2011-02-08 16:46:11

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 + ";})()");

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.

@Tahir Malik 2012-10-01 16:09:41

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

@ray6080 2013-09-10 11:58:18

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.

@Amal Murali 2014-07-30 15:40:46

Note for passerby: here's a good online tool to check if your JSON string is valid:

@Charles 2015-07-21 18:07:34

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?

@Jezen Thomas 2016-09-02 15:29:17

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

@Elenasys 2015-06-04 14:44:10

The easiest way using parse() method:

var response = '{"a":true,"b":1}';
var JsonObject= JSON.parse(response);

this is an example of how to get values:

var myResponseResult = JsonObject.a;
var myResponseCount = JsonObject.b;

@legendJSLC 2014-03-12 09:33:26

If you use jQuery, it is simple:

var response = '{"result":true,"count":1}';
var obj = $.parseJSON(response);
alert(obj.result); //true
alert(obj.count); //1

@Boris Parfenenkov 2014-04-01 14:44:37

The same answer is already presented on this question.

@legendJSLC 2015-05-11 14:04:59

sorry, I have not seen that above.

@Joke_Sense10 2013-10-01 07:42:33

The following example will make it clear:

var jsontext   = '{"name":"x","age":"11"}';
var getContact = JSON.parse(jsontext);
document.write( + ", " + getContact.age);

// Output: x, 11


You can also use the eval function. The following example is using the eval function:

var jsontext   = '{"name":"x","age":"11"}';
var getContact = eval('(' + jsontext + ')');
document.write( + ", " + getContact.age);

// Output: x, 11

Since the JSON.parse function is more secure and executes faster than the eval function, I recommend you to use JSON.parse function.

@Simon B. 2019-03-26 14:17:58

using eval() is DANGEROUS

@Teja 2013-06-06 03:51:51

You can either use the eval function as in some other answers. (Don't forget the extra braces.) You will know why when you dig deeper), or simply use the jQuery function parseJSON:

var response = '{"result":true , "count":1}'; 
var parsedJSON = $.parseJSON(response);


You can use this below code.

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

And you can access the fields using jsonObject.result and jsonObject.count.

@Sahar Ch. 2014-05-21 16:27:59

jsonObject.count in the console.log returns undefined. How should I call it?

@ha9u63ar 2013-04-04 10:38:37

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.

@Janaka R Rajapaksha 2014-05-20 16:13:03

eval() can compile and execute any javascript too. so you are at a potential security issue if using eval(). but json parser will recognize only json strings and compile them into javascript objects.

@El Ronnoco 2011-02-08 16:45:37

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


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

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

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

alert('result:'+result+' count:'+count);

@user192344 2012-03-13 01:34:40

eval cant handle a json string which return as HTML

@ThiefMaster 2012-05-09 11:20:22

IF it's html-encoded it's not JSON anymore.

@Marc Lehmann 2013-07-04 01:31:33

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

Related Questions

Sponsored Content

35 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

48 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 1806396 View
  • 6655 Score
  • 48 Answer
  • Tags:   json comments

36 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

22 Answered Questions

[SOLVED] How do I parse a string to a float or int?

38 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

33 Answered Questions

[SOLVED] For-each over an array in JavaScript?

9 Answered Questions

[SOLVED] Why can't Python parse this JSON data?

  • 2010-05-14 15:54:20
  • michele
  • 2542959 View
  • 1366 Score
  • 9 Answer
  • Tags:   python json parsing

34 Answered Questions

[SOLVED] What is the correct JSON content type?

14 Answered Questions

[SOLVED] Encode URL in JavaScript?

Sponsored Content