Serialize a form to json

I have a form with multiple fields, the post output is like this :

Array(
[query] => Array
    (

        [categories] => Array
            (
                [0] => 344
                [1] => 64
            ),
        [ages] => Array
            (
                [0] => 23
                [1] => 10
            )

    )

[post_type] => product
[section] => my_section
[action] => my_action
[page] => 1
)

So i want to send the form throught ajax with similar json structure, but when serialiasing my form and json.stringify the result i got different structure which will not working with my search engine class :

var datas = my_form.serializeArray();
console.log(JSON.stringify(datas));

/// OUTPUT
var j = [{  
      "name":"query[categories][]",
      "value":"344"
   },
   {  
      "name":"query[categories][]",
      "value":"64"
  },
   {  
      "name":"query[ages][]",
      "value":"23"
  } ....

For the json i need following structure :

{  "query":{  
  "categories":[  
     "344",
     "64"
  ]},
    "post_type":"product",
    "section":"my_section",
    "action":"my_action",
    "page":"1"
}

Is it possible to “flat” the json from the form to get the good structure? or is there any solution ? i really need the good structure.

Thank you

$.ajax({
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: $(form).serialize(),
            dataType: 'json',
            success: function (data) {

            },
            error: function (xhr, ajaxOptions, thrownError) {

            }
        });