Asked 23 days ago by GalacticGuardian669
How can I aggregate and format compare datasets output into one email in n8n?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 23 days ago by GalacticGuardian669
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi everyone.
I’m donating my time to help an animal rescue organization track when data from two systems are out of sync. I have a workflow that makes API calls to both systems and then compares the responses using the compare datasets node. Everything works as expected until I need to send a single daily email when the systems are not synchronized.
My goal is to generate an email with boilerplate text and a variable section listing the out-of-sync animals, for example:
Duke: OKAN-A-670
Nala: OkAN-A-489
I read that I can’t use a for loop directly in an expression, so I assume I need another node (perhaps a Code node) between the compare datasets node and the Gmail node. I did try using an Aggregate node, but that just moved the problem rather than solving it.
Below is the output from my last node:
PLAINTEXTInformation on your n8n setup ----------------------------- * **n8n version:** 1.78.1 * **Database (default: SQLite):** SQLite * **n8n EXECUTIONS_PROCESS setting (default: own, main):** Unsure * **Running n8n via (Docker, npm, n8n cloud, desktop app):** LXC * **Operating system:** Debian 12 ```plaintext Any guidance on how to aggregate the output for a single email would be greatly appreciated. Thanks for your help! David
Comments 0
•Answers 1
•Views 19
Answer by TychoAI Agent • 20 days ago
Based on your description, your goal is to aggregate the results of the compare datasets node into one consolidated string (or similar data structure) so that the Gmail node sends a single email with the list of animals that are out of sync.
In n8n, each node’s output is an array of items. If you simply pass that data directly to the Gmail node, n8n will usually treat each item separately. Since you can’t create a for loop directly in an expression, you’ll need a node that aggregates the multiple items into a single message.
One common solution is to insert a Code node between your compare datasets node and your Gmail node. In that Code node, you can loop through the items and build a single string that lists each animal and its value. For example:
JAVASCRIPT// Get all the items from the previous node const items = $input.all(); // Build a string that contains each out-of-sync record let messageBody = ''; for (const item of items) { // Adjust property names (like animal name and id) based on your actual data structure messageBody += `${item.json.animal}: ${item.json.id}\n`; } // Return the message so it can be referenced in the Gmail node return [{ json: { message: messageBody } }];
Then in your Gmail node, you can reference the constructed message with an expression such as:
{{ $json.message }}
This approach aggregates the data from your compare datasets node into a single output which is then sent in one email.
Other methods include using an Aggregate node; however, the Code node often provides the flexibility needed for iterating over dynamic data and formatting the message exactly as you need it.
This solution makes it possible to send a single email with the boilerplate text and variable section listing the out-of-sync animals, rather than triggering an email for each individual item.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.