Struggling with nested array iteration in n8n JavaScript function

I’m working on a project that involves processing data from a SaaS API using n8n. The API returns a large JSON object with nested arrays. I’ve managed to handle single-level arrays, but I’m stuck on how to properly iterate through the deeply nested ones.

Here’s a simplified version of my input data structure:

{
  "success": true,
  "articles": [
    {
      "id": "xyz789",
      "headline": "Sample Article",
      "snippet": "This is a brief summary.",
      "category": "general",
      "datePublished": "2023-04-15T12:30:00Z",
      "url": "https://example.com/article",
      "keywords": ["news", "tech", "ai"],
      "metadata": [
        {
          "type": "source",
          "details": {
            "name": "Example News",
            "reliability": "high"
          }
        }
      ]
    }
  ]
}

My current n8n function processes most fields correctly, but it’s not handling the nested ‘metadata’ array as I’d like. The ‘keywords’ array is working fine, but ‘metadata’ comes out empty.

How can I modify my function to properly handle these deeply nested arrays? I’m not very experienced with JavaScript, so any help would be appreciated!

I’ve been in your shoes, struggling with nested arrays in n8n. What worked for me was using a combination of reduce() and map() methods. Here’s a snippet that might help:

const processedData = items[0].json.articles.reduce((acc, article) => {
  const processedArticle = {
    ...article,
    metadata: article.metadata.map(meta => ({
      ...meta,
      details: Object.entries(meta.details).map(([key, value]) => ({ [key]: value }))
    }))
  };
  acc.push(processedArticle);
  return acc;
}, []);

This approach flattens the metadata while preserving its structure. It’s been reliable in my projects, handling various levels of nesting. Just make sure to adjust the field names to match your specific data structure. Also, consider adding some error handling for robustness.

I’ve encountered similar challenges with nested arrays in n8n. One effective approach is to use the Array.map() method combined with Object.entries(). Here’s a snippet that might help:

let processedArticles = items[0].json.articles.map(article => {
  return {
    ...article,
    metadata: article.metadata.map(meta => ({
      type: meta.type,
      ...Object.entries(meta.details).reduce((acc, [key, value]) => {
        acc[`details_${key}`] = value;
        return acc;
      }, {})
    }))
  };
});

This flattens the nested ‘details’ object within ‘metadata’, making it easier to work with in n8n. Adjust the field names as needed for your specific use case. Remember to handle potential undefined values to avoid errors.

hey there! i’ve dealt with similar issues before. for nested arrays like ‘metadata’, try using a recursive function. it can handle any level of nesting. something like:

function processNested(obj) {
if (Array.isArray(obj)) {
return obj.map(processNested);
} else if (typeof obj === ‘object’) {
// process object properties here
}
return obj;
}

hope this helps! lmk if u need more clarification