I am writing an application in NodeJS / ExpressJS.
I have a form like this:
<form method="post" action="/sendinfo">
<label>Name</label>
<input type="text" name="name" />
<label>Address</label>
<input type="text" name="address[number]" />
<input type="text" name="address[street]" />
<input type="text" name="address[suburb]" />
<input type="text" name="address[postcode]" />
<label>Phones</label>
<input type="text" name="phones[0]" />
<input type="text" name="phones[1]" />
</form>
Here is my route:
app.post('/sendinfo', function (req, res) {
res.render('done');
});
When the form is submitted, mine req.bodylooks like this:
{
"name": "Jonathan",
"address[number]": "1",
"address[street]": "Test St",
"address[suburb]": "TestSuburb",
"address[postcode]": "1234",
"phones[0]": "12345678",
"phones[1]": "87654321"
}
But I want this to look like this, with values nested inside properties or arrays:
{
"name": "Jonathan",
"address": {
"number": "1",
"street": "Test St",
"suburb": "TestSuburb",
"postcode": "1234",
},
"phones": [
"12345678",
"87654321"
]
}
How can i do this?
source
share