JavaScript nested array processing issues in n8n workflow automation

I’m working with n8n to process API responses from a third-party service. The data comes back as a large JSON structure with deeply nested arrays that I need to flatten and process through a JavaScript function node.

My input data structure looks like this:

{
  "status": true,
  "posts": [
    {
      "id": "xyz5678",
      "heading": "example content title",
      "description": "example content description",
      "visibility": "private",
      "createdAt": "2022-03-15T09:30:00.000+00:00",
      "url": "https://example_url_here",
      "topics": [],
      "keywords": [
        "term1",
        "term2",
        "term3",
        "term4"
      ],
      "metrics": [
        {
          "category": "engagement",
          "total": 8,
          "data": [
            "value1",
            "value2",
            "value3"
          ],
          "origin": "internal"
        }
      ]
    }
  ]
}

My processing function:

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

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

The metrics field ends up empty in my final output even though the keywords field processes correctly. How can I properly extract and format the nested array values from the metrics section?

I encountered this exact problem when building a content aggregation workflow last year. The root cause is that you’re mapping over entry.title which doesn’t exist in your metrics objects - they contain category, total, data, and origin properties instead.

What worked for me was debugging by logging the actual structure first. Add console.log(JSON.stringify(item[field], null, 2)) before your metrics processing to see what you’re actually working with.

For your use case, you probably want to extract the data array values since that seems to contain the actual metric content. Try replacing your metrics processing with:

if (field === 'metrics') {
  processedData[field] = item[field].flatMap(entry => entry.data).join('\n');
  continue;
}

This flattens all the data arrays from your metrics entries and joins them with newlines. If you need to preserve the relationship between category and data, use item[field].map(entry => entry.data.join(', ')).join('\n') instead.

yeah your accessing a property that dosnt exist in the metrics array. the .title part is whats breaking it since theres no title field in your data structure. i had this same issue with n8n last month and spent way too long debugging it lol. try changing the metrics line to just grab the category field instead: processedData[field] = item[field].map(entry => entry.category).toString().replace(/,/g, '\n'); that should fix the empty output problem

The issue is in your metrics processing logic - you’re trying to access a title property that doesn’t exist in your data structure. Looking at your JSON, the metrics objects have properties like category, total, data, and origin, but no title field.

I ran into something similar when processing analytics data. Try this modification for the metrics section:

if (field === 'metrics') {
  processedData[field] = item[field].map(entry => `${entry.category}: ${entry.total}`).join('\n');
  continue;
}

This will give you output like “engagement: 8” for each metrics entry. If you need the data array values instead, use entry.data.join(', ') or combine multiple fields like ${entry.category} - ${entry.data.join(', ')}. The key thing is making sure you’re referencing properties that actually exist in your nested objects.