How to combine and reformat output?

Describe the problem/error/question

Hi everyone. I’m donating my time to help an animal rescue organization better track when information from two systems are out of sync. I have a workflow that makes API calls to two different systems and compares the responses in a compare datasets node. So far, so good.

However, the end goal is to send an email on a daily basis that indicates when the two systems are not in sync. The problem I’m having is I can’t figure out how to take the output from the compare datasets node and send a single email with are out of sync.

The screenshot is of a Gmail node with the input to it on the left. The message body will be boiler plate with the variable section listing the animals that are out of sync. Something along the lines of this:

Duke: OKAN-A-670
Nala: OkAN-A-489

I read that I can’t do a for loop in an expression, so my guess is that I will need (at least) another node between the compare dataset node and the Gmail node. Maybe a code node?

I tried it with an aggregate node, but that just moved the problem.

Share the output returned by the last node

Information 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

Thanks for your help!

David

It’s not exactly “looping” but you can do what you want in a single JS expression. Take a look at the expression in the Edit Fields node.

You also need to toggle on the “Execute Once” option in the node settings or you’ll get duplicate output items.

1 Like

WOOT! That did the trick!

Seems like I better start learning javascript. :upside_down_face:

However, I now have a different problem: with the compare dataset node, there are 4 possible outcomes, but only 3 that I need to deal with: A & B match, A has items that B does not and B has items that A does not. In this case, A can have items that B doesn’t and B has items that A does not, can both be true.

I figured I could do a javascript ternary operator in the Gmail node like this:

{{ if $json.SLmessageBody ? $json.SLmessageBody : “” }}

{{ if $jsonPFmessageBody ? $json.PFmessageBody : “” }}

{{ if $json.messageBody ? $json.messageBody : “” }}

However, I get an error if there is no output from the any of the 3 coming out of the compare datasets node.

I inserted an if node after each of the 3 outputs from the compare datasets node so that I only reformat (thanks to you!) the output to be what I need it to be. I tried inserting a merge node that collected the output from all 3 if nodes, but it ended up sending out 3 emails.

So, I’m close, but not quite there…

Any thoughts on how I can send out the email formatted properly?

Thanks again for your help

David

My current workflow:

First, FYI, the ternary syntax doesn’t need the keyword “if” in front of it. The way you have it would probably show an error. Also, you might be able to reduce that to just someVariable || "" …because JS has all these quirky little syntax tricks - I only have a vague idea why that works.

Did you try a Merge node set to Combine + Combine By Position? As long as the incoming items/objects each have fields with different names, you should get only one output item/object with all of the unique fields.

Then you might need to add and enable the Include Unpaired Items option to get an object output anyway when/if one of your inputs doesn’t send an item in (shown by the path with the if node below).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.