Dynamic expression evaluation inside strings

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
1 Like

Hi @ori ,

It sounds like you need to include only the expression in the backets in your if statment. Here’s a workflow.

Does this help?

Best,

Robert

Hey, I appreciate your reply!

Sorry if my previous explanation misled you, allow me to try to further clarify my original question.

I would like to receive from an input param an expression, such as:
1==0
1 > 10
1 >= 9
then, i would like to have an if statement that its true and false paths are determined based on the result of the expression.

Since i get the expression from a parameter, it is necessary a string. Now, while inserting 1==0 inside the if field will work, when done dynamically (and therefore is a string), the $evaluateExpression function will output the string ‘as-is’ as its result.

a close equivalent of the situation is to hardcodedly put an expression inside the if node, but surround it with quotations.

{{ $evaluateExpression(1==0) }} → false
{{ $evaluteExpression(“1==0” }} → “1==0”
{{ $evaluteExpression($json.expression}} → “1==0” (when the value of that param is “1==0”)

thanks:)

Hi, @ori

I think you will need to parse the string in a Code node, or in the expression itself, depending on the complexity of the expression…

For example, this expression splits it into two numbers and evaluates:

{{ $json.ex.includes('==') ? 
   Number($json.ex.split('==')[0]) == Number($json.ex.split('==')[1]) : 
   $json.ex }}

And this is a more comprehensive case that handles multiple operators:

{{ (function() {
  const ex = $json.ex;
  const operators = ['==', '>=', '<=', '>', '<', '!='];
  
  for (const op of operators) {
    if (ex.includes(op)) {
      const [left, right] = ex.split(op).map(part => Number(part.trim()));
      
      switch(op) {
        case '==': return left == right;
        case '>=': return left >= right;
        case '<=': return left <= right;
        case '>': return left > right;
        case '<': return left < right;
        case '!=': return left != right;
      }
    }
  }
  
  return ex;
})() }}

here is a test:

1 Like

Apparently, you need to embrace the expression to evaluate in its own double-handlebars.

Note the escape character in front of the “inner” closing handlebars \}}.

4 Likes

Awesome @Olek , I tried that but didn’t work,

but this is the trick that makes it work,

:clap:t2:

3 Likes

It was a blind shot. Luckily escaping arbitrary characters doesn’t discard sequences of no special meaning.

3 Likes

Thank you both so much for the help, this works!

3 Likes

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