Use jmespath filter to get an information from array

Hello to you all.

I’m trying to filter information in a set node using jmespath, but I can’t find anything in the documentation about applying the filter to the data as I have… I don’t know if I need to transform it or if Preformatted textI need to use the function node to get the result I want.

As I tested in https://jmespath.org/ it looks like my filter is doing what it’s supposed to do, but I think I am missing something in the syntax in n8n.

The problem is that you supply the wrong incoming data for JMESPATH to operate on. $node can not be used standalone like this, and if it could, it would return all the data of all the nodes, which is not what you want.

What you want, is all the incoming items. So theoretically, the correct one would be: $input.all()
But I just found a big in combination with an option you have to set (Execute Once) so that your example makes sense. So we can not use that.

For that reason, what you have to use is actually $("Code").all(). That gets all the outcoming items of the node “Code”. But also then your query needs updating as you did not consider json under which all the data is in n8n (here the n8n data structure).

So what you would end up with would be:

{{ $jmespath($("Code").all(), "[?json.name=='Lenovo'].json.category_id")   }}

As the Code-Node outputs, multiple items will also the Set Node output the result multiple times. For that reason, it is important to activate underneath the node-settings “Execute Once” (which seems to cause problems with $input.all() and why we can not use it).

Here is the full example workflow:

1 Like

Thank you @jan

Glad to hear that it was helpful!

Have fun!

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