2

I'm trying to collect html form inputs to JSON.

I was trying to realize it with jQuery serializeArray which works ok for simple input names, but in case I have multidimensional inputs name like

<input type="text" name="product[0][name]" >

as an output of serializeArray I have list of object like

var result = $('#myform').serializeArray();
console.log( result );

0: Object
   name: "product[0][name]"
   value: "product 1"
   ...
1: Object
   name: "product[1][name]"
   value: "product 2"
   ...
...

also tried with JSON.stringify($('#myform').serializeArray()); but oputput again contain concated names as keys

[{"name":"product[0][name]","value":"product 1"}, ...

But I would like to parse it or use some other know function from jQuery or other library which will give me output in this way

{
  product: [
      0: {'name':'product 1'},
      1: {'name':'product 1'},
      2: {'name':'product 1'},
  ],
  some_other_filed: [
      0: {'key':'value'},
      1: {'name':'value'}
  ]
}

Dose anyone know solution for this situation?

Armen
  • 4,064
  • 2
  • 23
  • 40
  • Possible duplicate of [Serialize form data to JSON](http://stackoverflow.com/questions/11338774/serialize-form-data-to-json) – ovm Jun 22 '16 at 07:51
  • @OleViaud-Murat actually i tested it already with `JSON.stringify(frm.serializeArray());` it gives same names as output in json {"name":"product[0][name]","value":"Product"} but i need to have it separated as i mentioned in question – Armen Jun 22 '16 at 07:55

1 Answers1

2

You could use jQuery serializeJSON plugin.

$('#myform').serializeJSON();
console.log( result );
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34