Set variable depending on IF node

Hi! I would like to set a variable depending on IF statement.

Starting from a webhook call, we need to check the query and then set the variable depending on string content:

IF webhook query contains “OOF”, then SET variable1 to “FOO”;
IF webhook query contains “RAB”, then SET variable1 to “BAR”.

Then, I would need to use “variable1” inside a Postgres node query.

Example:

select * from customers where customer_id = ‘$variable1’;

Just as an example, in Bash this would look like this:

if [[ $query == “OOF” ]]; then
variable1=“FOO”
fi

if [[ $query == “RAB” ]]; then
viariable1=“BAR”
fi

query=“select * from customers where customer_id = $variable1;”

How can I achieve this in N8N?

Thanks in advance for your help!

The easiest would be to have two different Set-Nodes. One connected to each IF-Node.

2 Likes

You could use short if, make in set value2 expression and write sth like

{{$json["query"] == "OOF" ? "FOO" : "BAR"}}
2 Likes

Thanks @jan ! After many tries I ended using your suggestion successfully:

Where “Set2” does the real job, because it ends using the value that obtains from the TRUE statement. In Set2 node I ended using this expression to achieve that:

{{$node[“Set”].json[“tipo_pago”]}}{{$node[“Set1”].json[“tipo_pago”]}}

I’m not sure if this was exactly what you suggested previously, but it works… maybe there is a prettier way? Let me know any suggestions!

Thanks again!

Thank you for your suggestion @Shirobachi ! This seemed a pretty straightworward solution because it doesn’t require any IF node, but… unfortunately it didn’t worked at the end.

The condition doesn’t worked when I applied the expression to the whole raw query (including all the values, not just a specific key).

For example; the output of “$json[“query”]” is something like this:

parameter1=QWE&parameter2=RTY&parameter3=OOF

So if in this full query it contains “OOF”, I need to set “variable1” to “FOO”; but for some reason this doesn’t work when the full query is read.

Note: I need to check the FULL raw query (including keys and values) because the keys are in different orders/id’s/names each time.

I am very interested to make this work and to know further about your suggested solution, because it seems pretty straightforward. Any additional information will be appreciated. Thanks!

Try includes method: