Let's say we were building a response to a query for the patients with more than one heart (just something to fill in the ...)
//Create an object for the response
var patientMessage = {};
//Populate the response meta data
patientMessage.count = 3;
patientMessage.sender = 'AcmeEHR';
patientMessage.receipient = 'EmcaEHR';
patientMessage.date = '' + qie.formatDate('yyyyMMdd', qie.getSystemDate());
//Initialize an Array in the response object
patientMessage.patients = [];
//Create another object for the first patient
var patient = {};
patient.firstName = 'Unknown';
patient.lastName = 'Unknown';
patient.middleName = 'Unknown';
patient.alias = 'The Doctor';
patient.numberOfHearts = 2;
//Add the patient to the response objects array in the first or 0 position
patientMessage.patients[0] = patient;
//Add another patient but this time let's just initialize the
//second or 1 position in the array as an object directly
patientMessage.patients[1] = {};
//Then populate it
patientMessage.patients[1].firstName = 'Hag';
patientMessage.patients[1].lastName = 'Man';
patientMessage.patients[1].middleName = 'Fish';
patientMessage.patients[1].numberOfHearts = 4;
//Repeat for the third patient in the 2 position of the array
patientMessage.patients[2] = {};
patientMessage.patients[2].firstName = 'Squid';
patientMessage.patients[2].lastName = 'Man';
patientMessage.patients[2].numberOfHearts = 3;
qie.debug(JSON.stringify(patientMessage));
The above demonstrates two ways to add objects to an array in JSON. So our resulting JSON response contains an array of patients with more than one heart. Note: [] creates a new Array vs {} creates a new Object.
Anyone, care to add any comments regarding other characters besides The Doctor with more than one heart?