Hi everyone. I just got n8n setup yesterday, and have a question about the Compare Dataset node.
I wrote a python scripts that calls two different API endpoints which both return complex arrays and I iterate over the arrays and extract the id elements from each, turn them into sets and then use python set operators returns what’s in array1 that’s not in array2 like this:
diff = set(array1) - set(array2)
Can I do something similar using the Compare Dataset node with complex arrays or do I have to write some code? If I have to write some code, what would I use in n8n to do so?
I’m getting data from two different APIs in n8n using the HTTP Request node, but can’t figure out how to use the Compare Dataset node where the inputs are complex arrays.
It would be really slick if I can use the Compare Dataset node instead of having to write code!
What is the error message (if any)?
Please share your workflow
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)
Not sure if this helps but the “Compare datasets” node works with items. If you’re able to use the “split out” node like the example below, this could be one way to plug them in.
Hi Jim and thanks for the reply. As a test, I simplified the inputs and have a json object with only type of key. However, the compare dataset node is still showing that what’s in A is only in A and what’s in B, is only in B even though they are both identical!
I annotated the screenshot below to show that the node isn’t working properly. I even put a sort node to each flow before the compare node, just in case, but it’s still not comparing properly.
So, i have another issue though: I need to send an email with the results of the compare dataset, and I added a Gmail node after the compare. When I connected the compare to the Gmail node, I was getting an email for every result from the compare dataset. So, for example, if there were two elements in the A branch only, two emails were generated.
I inserted an Aggregator node in between the compare node and the Gmail node, and that got me closer in that I’m now getting a single email with multiple values, but not formatted the way I need them to be. The output of the compare node are a name and Id attribute and the names are together and so are the ids. Example:
Tux,Piper OKAN-A-68110,OKAN-A-68034
Instead, what I need is this:
Tux: OKAN-A-68110 Piper: OKAN-A-68034
I suspect the aggregator node is not the right node, but I’m not sure what would be the right type of node.
Here’s a screenshot of the Gmail node with the input on the left:
Re: multiple email sends, this is happening because of “item linking” - see Item linking concepts | n8n Docs. It’s a key n8n concept to learn to get the most out of n8n.
You came pretty close! Aggregate node is indeed one way to solve your issue. Just switch the mode to “All items in a single list” instead which you can then loop through in the gmail node.
So, I’m getting closer, but not quite there. You said I could loop over the data in the Gmail node but, from what I’ve read in the docs, doing a loop in an n8n expression won’t work because it all has to be on one line. For example, this resulted in a syntax error:
{{ for (let i = 0; i < $json.data.length; i++) { text += data[i].NAME + “ ”;} }}
Am I misunderstanding the docs or is there a way to loop over the elements in the Gmail node?
Here’s a screenshot of the Gmail node showing the input on the left and in the email body, the two keys that I added to the message. However, it’s only grabbing the key of the index that I grabbed.
I also read up on a number of core nodes, but did not see anything that made sense to insert between the aggregator node and the Gmail node to address the issue.
Thanks to your help, I’m either getting really close, or I have a ways to go…