Is JSON.stringify() supported by IE 8?

To answer the question in the title directly, yes IE8 supports JSON.stringify() natively.

IE8 is the first version of IE to get this support, and the functionality is explained in detail by the dev team here: http://blogs.msdn.com/b/ie/archive/2008/09/10/native-json-in-ie8.aspx

The answer the second part of the question, yes you would need to include alternate functionality for IE6/IE7. Something like Modernizr can make it easy to check this.

Also note if the user is in Compatibility View in IE8, the JSON object will not be available.


If you try JSON.stringify() using IE 8 you need to ensure it is not working in compatibility mode. See JSON object undefined in Internet Explorer 8

You'll need to add

<meta http-equiv="X-UA-Compatible" content="IE=8" />

to your page


There's a better solution...

This doesn't directly answer your question, it provides a complete solution to your problem instead.

The jquery-json library provides a wrapper that uses the native JSON object implementation if it's available and falls back to it's own JSON implementation if it isn't. Meaning it'll work in any browser.

Here's the Usage Example from the Project's home page:

var thing = {plugin: 'jquery-json', version: 2.3};

var encoded = $.toJSON( thing );
// '{"plugin":"jquery-json","version":2.3}'
var name = $.evalJSON( encoded ).plugin;
// "jquery-json"
var version = $.evalJSON(encoded).version;
// 2.3

The usage is very simple: toJSON stringifies the JS source; evalJSON converts JSON string data back to JavaScript objects.

Of you look at the source, the implementation is surprisingly simple but it works very well. I have used it personally in a few projects.

There's no need to do browser detection if it works in every browser.


put following code in your js file ;

var JSON = JSON || {};

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {

var t = typeof (obj);
if (t != "object" || obj === null) {

    // simple data type
    if (t == "string") obj = '"'+obj+'"';
    return String(obj);

}
else {

    // recurse array or object
    var n, v, json = [], arr = (obj && obj.constructor == Array);

    for (n in obj) {
        v = obj[n]; t = typeof(v);

        if (t == "string") v = '"'+v+'"';
        else if (t == "object" && v !== null) v = JSON.stringify(v);

        json.push((arr ? "" : '"' + n + '":') + String(v));
    }

    return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};

// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
if (str === "") str = '""';
eval("var p=" + str + ";");
return p;
 };