Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:54:09+00:00 2026-05-27T07:54:09+00:00

If I have the following form: <form> <input name=foo value=bar> <input name=hello value=world> <input

  • 0

If I have the following form:

<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">
</form>

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

foo=bar&hello=world&animals%5B%5D=panda&animals%5B%5D=koalacar&%5Bmake%5D=Honda&car%5Borigin%5D=Japan

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?


EDIT

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"] ... }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:54:10+00:00Added an answer on May 27, 2026 at 7:54 am

    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.

    E.g.,

    <!-- 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
    {
      "honey":{
        "badger":"a"
      },
      "wombat":["b"],
      "hello":{
        "panda":["c"]
      },
      "animals":[
        {
          "name":"d",
          "breed":"e"
        }
      ],
      "crazy":[
        null,
        [
          {"wonky":"f"}
        ]
      ],
      "dream":{
        "as":{
          "vividly":{
            "as":{
              "you":{
                "can":"g"
              }
            }
          }
        }
      }
    }
    

    Usage

    $('#my-form').serializeObject();
    

    The sorcery

    (function($){
        $.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_]+$/
                };
    
    
            this.build = 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
                if(!patterns.validate.test(this.name)){
                    return;
                }
    
                var k,
                    keys = this.name.match(patterns.key),
                    merge = this.value,
                    reverse_key = this.name;
    
                while((k = keys.pop()) !== undefined){
    
                    // adjust reverse_key
                    reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), '');
    
                    // push
                    if(k.match(patterns.push)){
                        merge = self.build([], self.push_counter(reverse_key), merge);
                    }
    
                    // fixed
                    else if(k.match(patterns.fixed)){
                        merge = self.build([], k, merge);
                    }
    
                    // named
                    else if(k.match(patterns.named)){
                        merge = self.build({}, k, merge);
                    }
                }
    
                json = $.extend(true, json, merge);
            });
    
            return json;
        };
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
I have the following in my form <td><input id=Notifications_0__IssueCreate name=Notifications[0].IssueCreate type=checkbox value=true /><input name=Notifications[0].IssueCreate
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have the following form code: # forms.py class SomeForm(forms.Form): hello = forms.CharField(max_length=40) world
I have a form that takes the following inputs: Name: IBM Surface(in m^2): 9
I have the following text input on a budget calculator form which displays the
I have a structure like the following: <form> <input type=text /> <input type=text />
I have the following form <form name=myForm id=myForm method=post enctype=multipart/form-data action=script.php> and this jQuery
I have a select list like the following inside of a form. <select name=0[voicemail]
I have following UI part on JSF - it's simple search form with input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.