Ravinder Singh
Ravinder Singh

Reputation: 173

How to create a javascript array to send data into this json format?

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

Upvotes: 2

Views: 41

Answers (3)

Syafiq Kamarul Azman
Syafiq Kamarul Azman

Reputation: 862

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]

Upvotes: 0

ozil
ozil

Reputation: 7117

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>');

Upvotes: 1

Yoann
Yoann

Reputation: 3060

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.

Upvotes: 0

Related Questions