0

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?

3
  • Where exactly is the key "ABC" coming from in your example? Why do you need it?
    – esqew
    Commented Oct 29, 2019 at 21:00
  • Well, how do you know that the property name should be "ABC"? What about what fields go into the "Group"? Commented Oct 29, 2019 at 21:01
  • Create the object you want, var x = { "ABC": data } then do stringify on that. JSON.stringify(x).
    – nurdyguy
    Commented Oct 29, 2019 at 21:02

3 Answers 3

1

If you're using the arbitrary key "ABC" as per your example, you can just wrap your data structure into that pre-defined parent object:

var data = {
  "name": "Demo",
  "REQID": "1234aa",
  "label": "123",
  "contractTypeID": "321",
  "contractID": "1234",
};

var modifiedData = {

  "ABC": {
    "name": data.name,
    "REQID": data.REQID,
    "Group": {
      "label": data.label,
      "contractTypeID": data.contractTypeID,
      "contractID": data.contractID
    }
  }

};

console.log(modifiedData);

2
  • Thank for your help. And how about "Group"?
    – Alex
    Commented Oct 29, 2019 at 21:15
  • @Alex Missed that the first time around, in which case you can just manually map the keys to the values
    – esqew
    Commented Oct 29, 2019 at 21:30
1

Try

var data = { 
  ABC: {
    name: $("#name").val().trim(),
    REQID: $("#requestID").val().trim(),
    Group: {
      label: $("#label").val().trim(),
      contractTypeID: $("#contractTypeID").val().trim(),
      contractID: $("#contractID").val().trim(),                
    }
  }
}

console.log(JSON.stringify(data))
1

There are a few ways to do this. Another way is to build up your javascript object in stages like this:

var data = { ABC: { Group: {}}};
data.ABC.name = 'Demo';
data.ABC.REQID = '1234aa';
data.ABC.Group.label = '123';
data.ABC.Group.contractTypeID = '321';
data.ABC.Group.contractID = '1234';

console.log(JSON.stringify(data));

Just remember that javascript objects do not support order like javascript arrays do, so it won't look exactly how you specified it but this does produce the object you specified.

Here's a working fiddle.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.