I have an input form like this:
<div class="form-group">
<label class="text-info">name</label>
<input type="text" id="name" class="form-control" />
</div>
and a basic script like below:
$(document).ready(function () {
$("#postform").submit(function (e) {
e.preventDefault();
var data = {
name: $("#name").val().trim(),
requestID: $("#requestID").val().trim(),
label: $("#label").val().trim(),
contractTypeID: $("#contractTypeID").val().trim(),
contractID: $("#contractID").val().trim(),
}
console.log('{"ABC":' + JSON.stringify(data) + '}')
....
})
})
The results JSON with following:
{
"name": "Demo",
"REQID": "1234aa",
"label": "123",
"contractTypeID": "321",
"contractID": "1234",
}
But, I need result to be formatted like this:
{
"ABC":{
"name":"Demo",
"REQID":"1234aa",
"Group":{
"label":"123",
"contractTypeID":"321",
"contractID":"1234"
}
}
}
Is there a simple way to accomplish this or I will need to hardcode result?
"ABC"
coming from in your example? Why do you need it?var x = { "ABC": data }
then do stringify on that.JSON.stringify(x)
.