Sidebar

How can I create a JSON message with multiple patient records?

0 votes
354 views
asked Mar 16, 2016 by rich-c-2789 (15,250 points)

I need to create a JSON response that contains multiple patient records.  The receiving system expects something like this:

{
    "count": 3,
    "sender": "SendingSystem",
    "receipient": "ReceivingSystem",
    "date": "20160316",
    ...
    "patients": [
        {
            "firstName": "First",
            "lastName": "Last",
            "middleName": "Middle",
            ...
        },
        {
            "firstName": "First",
            "lastName": "Last",
            "middleName": "Middle",
            ...
        },
        {
            "firstName": "First",
            "lastName": "Last",
            "middleName": "Middle",
            ...
        }
    ]
}

1 Answer

0 votes
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.
 
This is the output from the debug:
 
{
    "count": 3,
    "sender": "AcmeEHR",
    "receipient": "EmcaEHR",
    "date": "20160316",
    "patients": [
        {
            "firstName": "Unknown",
            "lastName": "Unknown",
            "middleName": "Unknown",
            "alias": "The Doctor",
            "numberOfHearts": 2
        },
        {
            "firstName": "Hag",
            "lastName": "Man",
            "middleName": "Fish",
            "numberOfHearts": 4
        },
        {
            "firstName": "Squid",
            "lastName": "Man",
            "numberOfHearts": 3
        }
    ]
}
 
Anyone, care to add any comments regarding other characters besides The Doctor with more than one heart?
answered Mar 16, 2016 by rich-c-2789 (15,250 points)
...