Describe the issue/error/question
I have an API that is sending me this JSON. And I’m not sure the following is correct or if there is a better way
[{
"headers": {
"host": "stellium.app.n8n.cloud",
"x-request-id": "5c4695eeebcd0d873f22cce07cff598b",
"x-real-ip": "xxxxx",
"x-forwarded-for": "xxxxx",
"x-forwarded-host": "xxx.app.n8n.cloud",
"x-forwarded-port": "443",
"x-forwarded-proto": "https",
"x-forwarded-scheme": "https",
"x-scheme": "https",
"content-length": "1746",
"user-agent": "mailparser.io",
"accept": "*/*",
"content-type": "application/x-www-form-urlencoded"
},
"params": {},
"query": {},
"body": {
"id": "064a319f077b26c475bc7ab44184414a",
"received_at": "2023-02-28 16:56:13",
"processed_at": "2023-02-28 16:56:16",
"externalrecordid": "INT-220407147234",
"injuredparty": "No",
"clientfirstname": "Test",
"clientlastname": "Tester",
"clientmobilephone": "+523223214111",
"clientemail": "[email protected]",
"clientdob": "1969-03-06",
"injuredgender": "Male",
"clientstreet": "Test 200 # 45",
"clientcity": "La TestCity",
"clientstate": "La Teststate",
"clientpostal": "48400",
"casetype": "Johnson",
"injuredfirstname": "Test",
"injuredlastname": "Testerson",
"injuredmobile": "+523223212222",
"injuredemail": "[email protected]",
"injureddob": "1988-03-06",
"injuredstreet": "Test 200 # 45",
"injuredcity": "La Test Injured",
"injuredstate": "Jalisco",
"injuredpostal": "48400",
"attachments[0][att_id]": "fake",
"attachments[0][att_name]": "Luis_Questionnaire.docx",
"attachments[0][att_url]": "https://files.mailparser.io/f/fakesurl",
"attachments[0][att_size]": "14650",
"attachments[0][att_mime]": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"attachments[1][att_id]": "fake",
"attachments[1][att_name]": "Luis_Questionnaire__1_.docx",
"attachments[1][att_url]": "https://files.mailparser.io/f/fakeurl",
"attachments[1][att_size]": "14650",
"attachments[1][att_mime]": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"attachments[2][att_id]": "fake-lg",
"attachments[2][att_name]": "Luis_Signed_Docs.pdf",
"attachments[2][att_url]": "https://files.mailparser.io/f/fakeurl-lg",
"attachments[2][att_size]": "822101",
"attachments[2][att_mime]": "application/pdf",
"tempmarketingid": "a344o000000IBrDAAWzzzzz"
}
}]
The problem I’m having is getting the attathcments keys. This is in the beggining node and one call for every object. I’m trying to do something like this, but i’m getting an error when trying to get attachment values
let output = [];
let parserData = $('Receive call from Mailparser').first().json.body;
let arrayIndex = 0;
let validKey = true;
console.log(parserData.attachments[0])
// do {
// output.push({ attachmentURL: parserData.attachments[arrayIndex][att_url], attachmentName: parserData.attachments[arrayIndex][att_name]})
// arrayIndex = arrayIndex + 1;
// if(parserData.attachments[arrayIndex] != undefined || parserData.attachments[arrayIndex] != ''){
// validKey = true;
// } else {
// validKey = false;
// }
// } while (validKey = true);
// console.log(result);
// console.log(parserData.attachments.length);
return output
What is the error message (if any)?
ERROR: Cannot read properties of undefined (reading ‘0’) [line 8]
Share the output returned by the last node
What I’m trying to get for output is
[
{
"attchmentUrl":"http://url1",
"attachmentName":"attatchment1"
},
{
"attchmentUrl":"http://url2",
"attachmentName":"attatchment2"
}
]
Information on your n8n setup
- Running n8n via [n8n.cloud]: