JavaScript nested array processing issue in n8n workflow

I’m working with an n8n automation that processes API data from a service. The API returns large JSON responses with complex nested structures. My JavaScript function node handles simple arrays fine, but I’m having trouble with deeply nested array data.

Here’s a sample of the incoming JSON structure:

{
  "status": "ok",
  "posts": [
    {
      "id": "xyz9876",
      "headline": "sample content title",
      "description": "sample content description",
      "visibility": "open",
      "createdAt": "2021-08-12T10:30:00.000+00:00",
      "url": "https://example_url_here",
      "topics": [],
      "keywords": [
        "term1",
        "term2",
        "term3",
        "term4",
        "term5"
      ],
      "metrics": [
        {
          "category": "sample metric type",
          "total": 6,
          "items": [
            "item one",
            "item two",
            "item three",
            "item four",
            "item five",
            "item six"
          ],
          "origin": "external"
        }
      ]
    }
  ]
}

My current processing function looks like this:

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

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

The result shows empty values for the metrics field when exported to Google Sheets. How can I properly extract and format the nested array values so they display correctly in the final output?

The issue extends beyond just the property name mismatch that Liam23 mentioned. Your metrics processing logic has a fundamental flaw - you’re trying to map over the metrics array but only extracting one property when each metric object contains multiple nested arrays and values. For the structure you’ve shown, you’ll want to flatten the nested data more comprehensively. Try modifying your metrics handling to something like entry[field].map(metric => ${metric.category}: ${metric.items.join(', ')}).join('\n') to capture both the category and the actual items array. This will give you meaningful output instead of empty values. Also consider that the total property might be useful to include in your final output depending on your use case.

looks like your trying to access info.title but in your sample data the metrics objects dont have a title property - they have category instead. try changing info.title to info.category and see if that fixes the empty values issue

I’ve encountered similar nested array issues in n8n workflows before. The root cause is that your processing logic assumes a different data structure than what’s actually being returned. Beyond the property name issue, you’re not handling the nested arrays properly. When working with complex API responses like this, I found it helpful to add some debugging first - try logging console.log(JSON.stringify(entry[field], null, 2)) before processing to see exactly what structure you’re dealing with. For your specific case, the metrics array contains objects with nested items arrays, so you need to decide how to flatten this data. I typically handle this by creating a more explicit processing function that checks for the actual structure rather than assuming property names. Also worth noting that Google Sheets has limitations with how it displays complex nested data, so you might want to consider flattening everything into separate columns rather than trying to cram nested arrays into single cells.