2

I am new to this so please bear with me.

Please guide me as to how to create a javascript array which in turn gives me a json like this.

{
"MotorInsurance": [{
    "Service": "Compare",
    "Data": [{
        "Apikey": "1234",
        "Process": "Compare",
        "TransactionId": "32",
        "Type": "New",
        "Channel": "1"
    }],
    "Vehicle": [{
        "VehicleCode": "456",
        "RTOCode": "AP12",
        "RegistrationYear": "2016"
    }],
    "User":[{
         "IPAddress": "66",
         "DateTime": "12-06-2016"
    }]
}]

}

I have tried this :

var formData = {};
formData['MotorInsurance'] = {};
formData['MotorInsurance']['Service'] = "Compare";
formData['MotorInsurance']['Data'] = {};
formData['MotorInsurance']['Data']['Apikey'] = '1234';
formData['MotorInsurance']['Data']['Process'] = 'Compare';
formData['MotorInsurance']['Data']['TransactionId'] = '32';
formData['MotorInsurance']['Data']['Type'] = 'New';
formData['MotorInsurance']['Data']['Channel'] = '1';
formData['MotorInsurance']['Vehicle'] = {};
formData['MotorInsurance']['Vehicle']['VehicleCode'] = '';
formData['MotorInsurance']['Vehicle']['RTOCode'] = '';
formData['MotorInsurance']['Vehicle']['RegistrationYear'] = '';
formData['MotorInsurance']['User'] = {};
formData['MotorInsurance']['User']['IPAddress'] = '66.12.5.4';
formData['MotorInsurance']['User']['DateTime'] = '12-06-2016';

Please guide me. Thanks

2
  • What you have in the first example is an object. If you want to turn that in to JSON, just call JSON.stringify on it: jsfiddle.net/RoryMcCrossan/qqko91yg Commented Jun 22, 2016 at 10:51
  • MotorInsurance, Data, Vehicle and User are arrays. Commented Jun 22, 2016 at 10:56

3 Answers 3

1

var formData = {};
formData['MotorInsurance'] = [{}];
formData['MotorInsurance'][0]['Service'] = "Compare";
formData['MotorInsurance'][0]['Data'] = [{}];
formData['MotorInsurance'][0]['Data'][0]['Apikey'] = '1234';
formData['MotorInsurance'][0]['Data'][0]['Process'] = 'Compare';
formData['MotorInsurance'][0]['Data'][0]['TransactionId'] = '32';
formData['MotorInsurance'][0]['Data'][0]['Type'] = 'New';
formData['MotorInsurance'][0]['Data'][0]['Channel'] = '1';
formData['MotorInsurance'][0]['Vehicle'] = [{}];
formData['MotorInsurance'][0]['Vehicle'][0]['VehicleCode'] = '';
formData['MotorInsurance'][0]['Vehicle'][0]['RTOCode'] = '';
formData['MotorInsurance'][0]['Vehicle'][0]['RegistrationYear'] = '';
formData['MotorInsurance'][0]['User'] = [{}];
formData['MotorInsurance'][0]['User'][0]['IPAddress'] = '66.12.5.4';
formData['MotorInsurance'][0]['User'][0]['DateTime'] = '12-06-2016';
document.write('<pre>' + JSON.stringify(formData, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

Comments

0

Your formData['MotorInsurance'] has to be an array:

formData['MotorInsurance'] = [];

Then you'll be creating everything else in the first element of this array:

formData['MotorInsurance'][0] = {};
formData['MotorInsurance'][0]['Service'] = "Compare";

The same goes for Data, Vehicule and User.

Comments

0

You can approach it like this: for the nested objects, populate them one by one (like the data variable below) and then when assigning to the form data, make sure you put square brackets around the variable.

vehicle = {
  "VehicleCode": "456",
  "RTOCode": "AP12",
  "RegistrationYear": "2016"
};

user = {
  "IPAddress": "66",
  "DateTime": "12-06-2016"
};

data = {
  "Apikey": "1234",
  "Process": "Compare",
  "TransactionId": "32",
  "Type": "New",
  "Channel": "1"
};

service = {"Service": "Compare"}

o = {"Data": [data], "Vehicle": [vehicle], "User": [user], "Service": service}

formData["MotorInsurance"] = [o]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.