Using $evaluteExpression for dynamic comparisons

I have a workflow which gets as input equality/inequality expressions which i want to act based upon. I wish to evaluate the value of the expression inside the ‘if’ node and act accordingly. I have seen that theres a utility function $evaluteExpression - but it does not work when i attempt to evalute the whole expression as a string.

For example:
“if {{ $evaluteExpression(1==0) }}” will result in
“if false”
however, the following expression:
“if {{ $evaluteExpression(“1==0” }}” (notice the quotes) will result in
“if 1==0”

my expression is injected from a input parameter, therefore its necessarily a string.

I understand this is probably possible using the code block, but i would very much like to avoid it. Does anyone else have an idea?

  • n8n version: 1.86.0
  • Database: postgresdb

This is a classic problem when working with dynamic expressions like strings in n8n, especially if they come as input (e.g. from a table, form, webhook, etc.) and you need to evaluate them as real (boolean) conditions within the flow.

Using Node Code (more flexible and safer), but this is the only reliable way to evaluate a string as a logical expression in JS:

// inputData = { expression: “1 == 0” }

const expression = $json[“expression”]; // ex: “1==0”
let result;

try {
result = eval(expression); // CAUTION: eval can be unsafe with external input
} catch (err) {
result = false;
}

return [{ result }];

You can then use an IF node to check whether result is true or false.

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