JavaScript nested array processing issue in n8n workflow automation

Need help with complex array handling in n8n JavaScript node

I’m working with data from an API that returns deeply nested arrays and I can’t get my n8n function to process them properly. Single level arrays work fine but the complex nested ones are giving me empty results.

Here’s my input data structure:

{
  "status": "ok",
  "posts": [
    {
      "id": "xyz789",
      "headline": "sample content goes here",
      "description": "sample content description",
      "category": "private",
      "createdAt": "2022-03-15T10:30:00.000+00:00",
      "url": "example-url-here",
      "topics": [],
      "keywords": [
        "word1",
        "word2",
        "word3",
        "word4"
      ],
      "metrics": [
        {
          "kind": "sample metric type",
          "total": 8,
          "data": [
            "value one",
            "value two",
            "value three"
          ],
          "origin": "external"
        }
      ]
    }
  ]
}

My current processing code:

const output = []
for (const record of items) {
  output.push.apply(output, record.json.posts)
}

return output.map(item => {
  const processed = {};
  for (const field of Object.keys(item)) {
    if (field === 'keywords') {
      processed[field] = item[field].toString().replace(/,/g, '\n');
      continue;
    }
    if (field === 'metrics') {
      processed[field] = item[field].map(entry => entry.title).toString().replace(/,/g, '\n');
      continue;
    }
    processed[field] = typeof item[field] === 'object' ? JSON.stringify(item[field]) : item[field];
  }
  return { json: processed };
})

The metrics field comes out empty in my final output. How do I properly extract the nested array values so they show up correctly when I export to spreadsheets? The issue seems to be with accessing properties that don’t exist in the nested objects.

Your metrics mapping is looking for .title but those objects actually have .kind instead. Switch entry.title to entry.kind and you’re good to go. Might want to throw in some error checking too in case those nested arrays come back empty.

You’ve got two problems here: the property mismatch and how you’re handling nested data. First, change .title to .kind - but that’s just the start. You’re only grabbing one property and completely ignoring the data array where the actual values live. For spreadsheets, I’d flatten everything like this: item[field].map(entry => ${entry.kind}: ${entry.data.join(', ')}).join('\n'). You’ll get both the metric type and its data in a clean format. Don’t forget to add a null check before mapping - APIs love to return inconsistent structures. I hit the same wall with n8n processing social media analytics where nested arrays would just vanish randomly.

Luna23’s right about the property name issue, but there’s more. Your code assumes the metrics array will always have the expected structure - you’re not checking if the nested data array needs processing too. First, verify the metrics array exists with item[field] && item[field].length > 0 before mapping. Also decide if you want just the kind property or if you need to combine it with the data values from each metric object. For spreadsheet exports, you’ll probably want to flatten the entire metrics structure, including nested data arrays, instead of just grabbing the top-level kind field. I’ve hit similar problems with n8n when API responses vary between calls.