How to (completely) convert query string into JSON object?

If I have the following form:

  <input name="foo" value="bar">
  <input name="hello" value="world">
  <input name="animals[]" value="panda">
  <input name="animals[]" value="koala">
  <input name="car[make]" value="Honda">
  <input name="car[origin]" value="Japan">

I do not want to use $("form").serialize():


Instead, I want this:

{"foo":"bar", "hello":"world", "animals":["panda", "koala"], "car":{"make":"Honda", "origin":"Japan"}}

To my understanding, jQuery used to do this, but they switched the serialize method to return the GET-style query string. Is there an easy way to get my desired result?


I've updated my original question to include car[make] and car[origin] examples. It should be assumed that foo[bar][baz] or foo[bar][baz][bof] input could appear on the form as well.

Additionally, numerically indexed keys that are specified such as foo[0]=a, foo[1]=b, foo[4]=c should be preserved, e.g.,

{ ... "foo":["a", "b", undefined, undefined, "c"] ... }


Convert forms to JSON LIKE A BOSS

Github: Follow along on Github

The following code can take work with all sorts of input names; and handle them just as you'd expect.


<!-- all of these will work! -->
<input name="honey[badger]" value="a">
<input name="wombat[]" value="b">
<input name="hello[panda][]" value="c">
<input name="animals[0][name]" value="d">
<input name="animals[0][breed]" value="e">
<input name="crazy[1][][wonky]" value="f">
<input name="dream[as][vividly][as][you][can]" value="g">
// output
The sorcery
    $.fn.serializeObject = function(){

        var self = this,
            json = {},
            push_counters = {},
            patterns = {
                "validate": /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
                "key":      /[a-zA-Z0-9_]+|(?=\[\])/g,
                "push":     /^$/,
                "fixed":    /^\d+$/,
                "named":    /^[a-zA-Z0-9_]+$/
 = function(base, key, value){
            base[key] = value;
            return base;

        this.push_counter = function(key){
            if(push_counters[key] === undefined){
                push_counters[key] = 0;
            return push_counters[key]++;

        $.each($(this).serializeArray(), function(){

            // skip invalid keys

            var k,
                keys =,
                merge = this.value,
                reverse_key =;

            while((k = keys.pop()) !== undefined){

                // adjust reverse_key
                reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), '');

                // push
                    merge =[], self.push_counter(reverse_key), merge);

                // fixed
                else if(k.match(patterns.fixed)){
                    merge =[], k, merge);

                // named
                else if(k.match(patterns.named)){
                    merge ={}, k, merge);

            json = $.extend(true, json, merge);

        return json;

Need Your Help

Password file obfuscation

encryption lua obfuscation password-protection

I am using lua to administrate a firewall server and want to obfuscate sensible variables such as login data. I have tried luac but the variable content is still easily readable. Is there any way to

Passing a variable from vbscript to batch file

windows batch-file vbscript

i am very new towards batch programming and vbscripting

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.