Question: I'm trying to use the new FormData element to convert a form into one (nested) JSON string.

This is the example form structure:

1. name
2. nested[key]
3. nested[key2]
4. nested[key3]

Using the following code, I'll get one flat JSON object, not the nested version.

```js
    var formData = new FormData(document.getElementById("testForm"));
    var object = {};
    formData.forEach(function(value, key){
        object[key] = value;
    });
    var json = JSON.stringify(object);
    return json;

```

Result:

```
{"name":"test","nested[key]":"test","nested[key2]":"test","nested[key3]":"test"}

```

Answer: 
Have a look at the FormData documentation here: <https://developer.mozilla.org/en-US/docs/Web/API/FormData>

In my example below I've used the entries() method and I've converted it to an array be able to use the forEach method and build the object.

```js
// Get the form
var myForm = document.getElementById('myForm');
// Create FormData object
var formData = new FormData(myForm);

// Loop through the entries and append to the object
var result = {};
Array.from(formData.entries()).forEach(function(entr) {
  var key = entr[0];
  var value = entr[1];
  result[key] = value;
});

// Print the result
console.log(result);
```

```html
<form id="myForm">
Name <input name="name" value="a"/>
</form>
```
you can try `set` with lodash:

```js
import { set } from 'lodash';

var formData = new FormData(document.getElementById("testForm"));
var object = {};
formData.forEach(function(value, key){
    set(object, key, value);
});

```

set document here: <https://lodash.com/docs/4.17.14#set>