Pulling data from Array within API Call - Javascript

Hey guys,

I am working within my third party connector, I have an API that pulls contacts and their information. I need to pull emails in this but is displayed as an array and is presenting me with a blank dataset during testing.

"items": [
 {
   "id": 0,
   "title": "string",
   "firstName": "string",
   "lastName": "string",
   "emails": [
         "user@example.com"
   ],
}

For example I have an API call that is taking that API and creating a Schema and Table.

datagrid.addColumn('id', datagrid.DATA_TYPE_DOUBLE);
datagrid.addColumn('displayName', datagrid.DATA_TYPE_STRING);
datagrid.addColumn('firstName', datagrid.DATA_TYPE_STRING); // Above is the Schema and below is the Table // // process data row by row
for (var i = 0; i < table.length; i++) {
var data = table[i]; if (data.id) {
datagrid.addCell(data.id);
}
else { datagrid.addCell(null); } if (data.displayName) {
datagrid.addCell(data.displayName);
}
else { datagrid.addCell(null); }

if (data.firstName) {
datagrid.addCell(data.firstName);
}
else { datagrid.addCell(null); }

How would I add email into the above Table and Schema to pull the required information. There can be anywhere from 1 - 3 emails connected to each user within this array

Answers

  • It depends on how you want your data to be structured. You could duplicate the rows so that each email is on its own row or you'd have one record with three separate columns to store the three separate email addresses.

    In either case you'd need to use a for loop and loop through the emails property of your data.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • What would be the best way to go about doing this? I would like to have each email in a new column and have tried to loop through it but ive never had to loop through an array within and array before

    //maximum emails per contact is 2
    for (var emailsnum = 0; emailsnum < 2; emailsnum++) {
    datagrid.addColumn('contact' + '_email' + emailsnum, datagrid.DATA_TYPE_STRING);
    datagrid.addColumn('contact' + '_email' + emailsnum, datagrid.DATA_TYPE_STRING);
    }

    and

                // emailAddress (set to 2 maximum)
    if (data.emails) {
    datagrid.addCell(data.emails);
    }
    else { datagrid.addCell(null); }

    Im not sure how to format the table for it, but should this be working?
    I am still confused about looping through the Emails array within the normal dataset array