Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery.stringify() utility
/**
* converted stringify() to jQuery plugin.
* serializes a simple object to a JSON formatted string.
* Note: stringify() is different from jQuery.serialize() which URLEncodes form elements
* UPDATES:
* Added a fix to skip over Object.prototype members added by the prototype.js library
* USAGE:
* jQuery.ajax({
* data : {serialized_object : jQuery.stringify (JSON_Object)},
* success : function (data) {
*
* }
* });
*
* CREDITS: http://blogs.sitepointstatic.com/examples/tech/json-serialization/json-serialization.js
*/
jQuery.extend({
stringify : function stringify(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 (obj.hasOwnProperty(n)) {
if (t == "string") v = '"' + v + '"'; else if (t == "object" && v !== null) v = jQuery.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
}
});
@pedrochaves

This comment has been minimized.

Copy link

commented Mar 1, 2012

Hint:
You could optimize this method to use the native JSON.stringify method, so that your recursive method would only be executed on IE6/7, like this:
This way, your code would only be executed on older browsers, working as a fallback :)

jQuery.extend({
    stringify  : function stringify(obj) {         
        if ("JSON" in window) {
            return JSON.stringify(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 (obj.hasOwnProperty(n)) {
                    if (t == "string") {
                        v = '"' + v + '"';
                    } else if (t == "object" && v !== null){
                        v = jQuery.stringify(v);
                    }

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

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

This comment has been minimized.

Copy link

commented Sep 19, 2012

Thanks, to both of you! Super handy!

@ghost

This comment has been minimized.

Copy link

commented Oct 3, 2012

I'm about to use this.. RIGHT NOW! :D THANKS!

@sanfilippopablo

This comment has been minimized.

Copy link

commented Oct 17, 2012

Good.

@mcgarciabr

This comment has been minimized.

Copy link

commented Oct 24, 2012

Thanks both of you !!! Very good help.

@porub

This comment has been minimized.

Copy link

commented Jan 22, 2013

Doesn't escape double qoutes (") :(((.

So JSON.stringify( { Password: 'ab"c' } ) produces good JSON string: '{"Password":"ab"c"},
but the jQuery.stringify( { Password: 'ab"c' } ) in IE6-7 or in IE8+ compatibility mode leads to a broken unescaped string: '{"Password":"ab"c"}'

see also: http://code.google.com/p/jquery-json/source/browse/trunk/src/jquery.json.js

@borgle

This comment has been minimized.

Copy link

commented Aug 21, 2015

escape double qoutes. :)))

jQuery.extend({
    stringify  : function stringify(obj) {         
        if ("JSON" in window) {
            return JSON.stringify(obj);
        }

        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type
            if (t == "string") obj = '"' + obj.replace(/"/g,'\\\"') + '"';
            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 (obj.hasOwnProperty(n)) {
                    if (t == "string") {
                        v = '"' + v.replace(/"/g,'\\\"') + '"';
                    } else if (t == "object" && v !== null){
                        v = jQuery.stringify(v);
                    }
                    json.push((arr ? "" : '"' + n + '":') + String(v));
                }
            }

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

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.