Filtering with a value from a previous node

Hey everyone,

I’m pretty new to n8n and have a simple question regarding filtering inside the expression editor and was not sure if it’s possible or not. I would like to add a value from a previous node to a filtering process of the current node. Here is a short example:

Ideally, I would like to replace the value inside the second Set node with the variable from node 1. So something like: {{$jmespath($json.data, “[?email==‘Variable from Set1’].link | [0]”)}}.

Is this possible or do I have to build a different workflow? I would appreciate any kind of help.

Hi @Batijko, welcome to the community! This is possible, you can reference any previous node in your workflow using an expression such as {{ $node["Node name"].json["field"] }}.

You can write these expressions manually (which is especially useful when using them in a more complex context like in your JMESPath example) or simply click your way through the expression editor like so (after running your previous node):

Recording 2022-09-14 at 16.43.48

Thanks @MutedJam for your quick answer but maybe you didn’t get my question or I asked a little bit imprecise. I would like to refer the first node variable inside the filtering operation of JMESPath. So something like this:

Obviously, is the expression inside the second quotation marks wrong but is there a possiblity to do this somehow or do I have to use the Function node?

Got it @Batijko, sorry for the misunderstanding. I couldn’t get this to work in the Set node/using the expression editor, presumably as the underlying library is somewhat limited.

It did, however, work fine when using the Function or Function Item node using template literals this like so:

The relvant code snippet is this one:

item.result_expression = $jmespath($json.data, `[?email=='${$node["Dynamic mail"].json["mail"]}'].link | [0]`);

Result:

Is this what you had in mind?

Yes, that’s exactly what I wanted. Solved it already with a simple JS forEach function but your solution looks pretty easy as well. Thanks a lot for the help @MutedJam :+1:

1 Like

Sweet, glad to hear you figured it out! Tbh, most of the time I also prefer plain JS over JMES path :wink:

Hey @MutedJam I would have a small follow up question, if you could be so kind and help me out once again. I was playing around with JMESpath a little bit more and in the following workflow I have a small issue:

In the result node apart from item.result_dynamic_id I’m getting all the correctly filtered results. My guess would be that I have to set the right quotation marks because changing the ones inside item.result_hardcoded gives me an empty result as well. I tried all possible combinations without a success. Any idea how the correct expression has to be here to get a result?