Replace a variable based on a pre-defined array, possibily lookup?

Describe the problem/error/question

Not sure if this is a n8n limitation or just lack of understanding but we are trying to replace a variable based on a pre-defined array, as seen in the attached ‘dummy’ workflow.

For example: if dummySeach ‘id’ = ‘1’, then output ‘Mary’ or if dummySeach ‘id’ = ‘2’, then output ‘Bob’

What is the error message (if any)?

No error messages in non dummy workflow as can’t even get to that stage.

Please share your workflow

Share the output returned by the last node

Not able to get to that stage

Information on your n8n setup

  • n8n version: 0.223.0
  • Database (default: SQLite): what ever came with installer
  • n8n EXECUTIONS_PROCESS setting (default: own, main): no idea
  • Running n8n via (Docker, npm, n8n cloud, desktop app): desktop app from main website
  • Operating system: windows 10

Hi @ersatz.feign, this sounds like a job for JMESPath using which you can query data on-the-fly. Check out this example workflow:

The expression in use here is {{ $jmespath($('dummyInfo').all(), "[].json | [?id == '" + $ + "'] | [0]") }} which might look a bit complicated at first. So let me break it down:

  • $jmespath(): This is simply how $jmespath is used in n8n with the function requiring two parameters separated by a comma. The first parameter is the dataset we want to perform the search against, the second parameter describes the search.
  • $('dummyInfo').all(): Our dataset, containing all items from the dummyInfo node (docs).
  • [].json | [?id == '" + $ + "'] | [0]": Our search string. We’re essentially looking at the json field of each item from our source dataset ([].json), then search in the respective id fields for a value matching the id of our current item ([?id == '" + $ + "']). Finally we’re returning the first result assuming you’ll only ever have one match ([0], the index is zero-based, meaning the first item is 0, the second one would be 1, the third one would have index 2 etc.)

This example would return all fields if it finds a result:

But you can, of course, append .name.first to the above if you only want the first name and nothing else:

Hope this helps!


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