A recursive jQuery.toJson() function

By enhancing the code written here, I created a javascript toJson() function, which recursively converts any javascript object to json (javascript object notation).
The function relies on jQuery, and is added to the main jQuery object '$'. So, you can call $.toJSON(obj).

Installation is done by adding both jQuery.js and toJSON.js to the head of the html page, and off course calling $.toJSON(obj).

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 <script type="text/javascript" src="toJSON.js"></script>
 <script type="text/javascript">
  $().ready(function(){
   $('#someButton').click(function(){
    // jQuery.toJSON(object, compact, indent)
    $.toJSON(someObject, 1, 0);
   });
  });
 </script>

Code can be viewed and downloaded:

You can use the test page here as well:

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 8277 times
  1. Karl

    #1 by Karl - March 20, 2011 at 10:06 PM

    I am in need of a toJSON function like this you wrote two years ago. You said yours "cleaned up" the original jquery plugin which is now at:

    http://code.google.com/p/jquery-json/

    Your changes happened as best I can tell in 2009-06, while the latest jquery.json-2.2 released in 2009-08. It did not as far as I can tell incorporate your parameterized ability to compact and indent, which I want.

    Other than this apparent third implementation at javascriptMVC:

    http://javascriptmvc.com/docs/jQuery.toJSON.html#&who=jQuery.toJSON

    and a host of references to Crockford's Json2 suite

    https://github.com/douglascrockford/JSON-js

    these appear to be all the traces I can find to do a very simple thing - turn a series of form input elements into a predictable JSON string to save. I think its possible your code may still be the best bet, but I wanted to throw it back to you - do you know any better alternative now?
  2. Paul Klinkenberg

    #2 by Paul Klinkenberg - March 21, 2011 at 11:59 PM

    Hi Karl, if you simply want all form variables as a regular string, then you could also use jQuery: $('#commentform').serialize() for example.
    If you want the data to be a JSON string, then you'll have to take one of the json options you wrote, or another one for that matter. Haven't really looked into it that much.
    Cheers, Paul
  3. Karl

    #3 by Karl - March 24, 2011 at 3:47 PM

    Thanks Paul.

    I am not passing serialized data but a json string - your original concept for an improved version of that toJSON is exactly what I am after.

    I would use a version of your toJSON instead of the plugin jquery.json-2.2's version, except that yours does not work in Chrome for some reason, but does fine in FF and Safari - must be a webkit related bug.

    That plugin ( which you and others have tried unsuccessfully to improve) and the idea of generating formatted json from form (or other) structures seems strangely dormant, and I cannot figure out why everyone is shunning this avenue, which potentially allows the data, schema, and form definition to be consolidated into one json file for use with simple, reusable CRUD functions.
  4. JP Berry

    #4 by JP Berry - March 28, 2011 at 8:11 PM

    Karl / Paul,

    I also tested this in Google chrome and received an error when trying to serialize html elements.

    Check out line 128 :
    "for each(k in ['nodeName']){"

    Chrome does not support the "for each .. in" method.

    So, just change the method to something like this :

    [code]
    // * Google Chrome / Webkit does not support the "for each .. in" method
    // for each(k in ['nodeName']){
    // ret.push('"' + k + '":' + optionalSpace + $.quoteString(o[k]) );
    // }

    // ...So we'll do it the old fashioned way
    for (k in ['nodeName']) {
    var nodeValue = ['nodeName'][k];
    ret.push('"' + nodeValue + '":' + optionalSpace + $.quoteString(o[nodeValue]));
    }

    [/code]

    And that should do it. Hope this helps!
    -JP
  5. Karl

    #5 by Karl - March 29, 2011 at 10:46 PM

    Thanks JP. Tried it - seems to work great.
  6. Karl

    #6 by Karl - March 29, 2011 at 10:50 PM

    [Reset email to subscribe]
(will not be published)
Leave this field empty