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?