I am trying to extract the email field from a JSON file using JavaScript. Here is the snippet of code:
"contacts": [
{
"addedAt": 1332358711001,
"vid": 1,
"properties": {
"lastname": {
"value": "Mott"
},
"firstname": {
"value": "Adrian"
}
},
"identity-profiles": [
{
"vid": 1,
"identities": [
{
"type": "EMAIL",
"value": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f6e7f1f6afe4e6e4e1b4e1b0e7afe7b3bbe7afb6b3b1baafbab0b2b3afbab1b6b0e1e3b1b1b1e3e3b3c2eaf7e0f1f2edf6ace1edef">[email protected]</a>",
"timestamp": 1332358711715
},
{
"type": "LEAD_GUID",
"value": "f3ebaf07-1c6d-4ada-af31-3559dd8b3027",
"timestamp": 1332358711771
}
]
}
]
}]
The code successfully retrieves all fields except for Identities, where it returns NULL or unidentified.
var temp = fields.contacts.length;
for (var i=0; i<fields.contacts.length; i++){
var addedAt = fields.contacts[i].addedAt;
var formattedDate = Utilities.formatDate(new Date(addedAt), "GMT", "yyyy-MM-dd");
var lastName = fields.contacts[i].properties.lastname.value;
var firstName = fields.contacts[i].properties.firstname.value;
var vid = fields.contacts[i].vid;
var ip = fields.contacts[i]['identity-profiles'];
var id = ip.identities;
}
The variable id always returns unidentified. I also tried the following:
for (var j=0; i<id.length; j++){
if(typeof ['type'] == 'EMAIL'){
var email = id[j].value;
}
};