Issue extracting data from array code node

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]:

I’ve tried pulling into a set “just to see”. It sees the value in the schema on the input, but the output it doesn’t see anything there.

Is the JSON really like the pasted JSON above? Normally those attachments should be in an array, here the array notation is in the name of the properties.

If you use {{ $json.body["attachments[1][att_id]"] }} (put the whole name including array notation in quotation marks, then it’ll work)

Screenshot

But that’s not really a satisfying solution, as the notation should be like you used it in the Screenshot.

Yes that the direct input from the Mail Parser I’m using. I found an option on that api to change to a flat data structure and I think that’s the route I’m going to end up having to go. Output below. Just need to figure out now how to get the output I need with name and url together in an array of objaects

{
	"attachments_att_id": "fake",
	"attachments_att_name": "Luis_Questionnaire.docx",
	"attachments_att_url": "https://files.mailparser.io/f/fake",
	"attachments_att_size": "14650",
	"attachments_att_mime": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
	"attachments_att_id_2": "fake",
	"attachments_att_name_2": "Luis_Questionnaire__1_.docx",
	"attachments_att_url_2": "https://files.mailparser.io/f/fake"
}

So now I’m stuck on how I can dynamically get the key values I may have 1 or I may have 5 attachments or anything inbetween

Can you share your workflow? Especially the mail node would be interesting.
There has to be some way to get the JSON with a dedicated array for the attachments.

If nothing works, you could do something like following flow:

this flow is based on the “not flattened” JSON, the code node extracts all keys which contain “attachment” and “att_url” (or whatever field you need) and puts them into an array.

Then the “split into batches” node splits multiple mails so only one mail gets worked on at a time, you do not need this if you get a max of 1 mail.

The “Item List” node splits the attachments array into multiple n8n items, with that you can do something for each attachment separately.

But that work around is anything but the yellow of the egg (as we like to say in German :P) maybe we can get behind the root problem of the mail node returning a falsely formatted JSON.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.