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 6913603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:14:22+00:00 2026-05-27T09:14:22+00:00

I have a JSON object as {a: 1, b: 2, c: [4, 5, 6],

  • 0

I have a JSON object as {a: 1, b: 2, c: [4, 5, 6], d: {x: 7, y: 8, z: [9, 0]}}, how do I convert it to a form of elements?

I want to convert to this:

<input type="hidden" name="obj[a]" value="1"/>
<input type="hidden" name="obj[b]" value="2"/>
<input type="hidden" name="obj[c][]" value="4"/>
<input type="hidden" name="obj[c][]" value="5"/>
<input type="hidden" name="obj[c][]" value="6"/>
<input type="hidden" name="obj[d][x]" value="7"/>
<input type="hidden" name="obj[d][y]" value="8"/>
<input type="hidden" name="obj[d][z][]" value="9"/>
<input type="hidden" name="obj[d][z][]" value="0"/>

Any ideas?

  • 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-27T09:14:23+00:00Added an answer on May 27, 2026 at 9:14 am

    These should do it:

    with jQuery

    var obj = { a: 1, b: 2, c: [4, 5, 6], d: { x: 7, y: 8, z: [9, 0] } };
    
    function analyzer(o, trail) {
        if (!trail) trail = 'obj';
        for (var n in o) {
            var el = $('<input>', { type: 'hidden' });
            el.attr('name', trail + '[' + n + ']');
            if (typeof o[n] === 'number') {
                el.attr('value', o[n]);
                $('body').append(el);
            } else if (Array.isArray(o[n])) {
                el.attr('name',function(i,v) { return v + '[]'; });
                for (var i = 0; i < o[n].length; ++i) {
                    el.clone().attr('value', o[n][i] ).appendTo('body');
                }
            } else {
                analyzer(o[n], trail + '[' + n + ']');
            }
        }
    }
    analyzer(obj);
    

    JSFIDDLE DEMO

    FYI, you can find a compatibility patch for Array.isArray at MDN.

    Or you can use jQuery.isArray() instead.


    jQuery reworked

    You may prefer this a bit. It doesn’t create an element, then clone it in the Array loop, but rather uses a little redundant code to create the elements.

    function analyzer(o, trail) {
        if (!trail) trail = 'obj';
        for (var n in o) {
            if (typeof o[n] === 'number') {
                $('<input>', { type: 'hidden', name: trail + '[' + n + ']', value: o[n] })
                    .appendTo( 'body' );
            } else if (Array.isArray(o[n])) {
                for (var i = 0; i < o[n].length; ++i) {
                    $('<input>', { type: 'hidden', name: trail + '[' + n + '][]', value: o[n][i] })
                        .appendTo( 'body' );
                }
            } else {
                analyzer(o[n], trail + '[' + n + ']');
            }
        }
    }
    

    JSFIDDLE DEMO


    with native DOM API

    If you’re having any performance issues, use the native API, and cache the DOM selection.

    function analyzer(o, trail) {
        var el;
        if (!trail) trail = 'obj';
        for (var n in o) {
            if (typeof o[n] === 'number') {
                el = document.createElement('input');
                el.type = 'hidden';
                el.name = trail + '[' + n + ']';
                el.value = o[n];
                container.appendChild( el );
            } else if (Array.isArray(o[n])) {
                for (var i = 0; i < o[n].length; ++i) {
                    el = document.createElement('input');
                    el.type = 'hidden';
                    el.name = trail + '[' + n + '][]';
                    el.value = o[n][i];
                    container.appendChild( el );
                }
            } else {
                analyzer(o[n], trail + '[' + n + ']');
            }
        }
    }
    

    JSFIDDLE DEMO

    Doesn’t require much more code, should work in all typically supported browsers, and should be very fast.


    native API reworked to shorten

    function analyzer(o, trail) {
        var el;
        if (!trail) trail = 'obj';
        for (var n in o) {
            (el = document.createElement('input')).type = 'hidden';
            el.name = trail + '[' + n + ']';
            if (typeof o[n] === 'number') {
                container.appendChild( el ).value = o[n];
            } else if (Array.isArray(o[n])) {
                el.name += '[]';
                for (var i = 0; i < o[n].length; ++i) {
                    container.appendChild( el.cloneNode(false) ).value = o[n][i];
                }
            } else {
                analyzer(o[n], trail + '[' + n + ']');
            }
        }
    }
    

    JSFIDDLE DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form field which requires a json object as its value when
I have a JSON object holding the following value: @value = {val:test,val1:test1,val2:test2} I want
I have a problem like this Convert an HTML form field to a JSON
I have a JSON object and I want to convert it escaping / characters.
I have a json object of this type: jsonObject={visits:'3', v1:{'timestamp':'1231313.311', 'time_year':'2011', 'time_month':'Jan', 'time_date':'15' 'X_value':'X'
I have a javascript object of this form obj = [ { title: Sean
I want to convert the html tag objects to json object in the javascript
How to convert a ruby hash object to JSON? So I am trying this
I have this JSON string: { success:true,user_id:309,id:309,sessId:false,email:null,name:Mai Van Quan,username:quanmv,role:Reseller Admin,messages:,org_name:null,microPayNumber:4949,microPayWord:neocam,mobile:null,permissions:{ADD_CAMERA:true, REMOVE_CAMERA:true, EDIT_CAM_GENERAL:true, ACCESS_CAM_TECHNICAL:true, EDIT_CAM_PKG:true,
I can have Spring convert my json POST submission into an object with a

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.