Home » n8n» Convert types in n8n IF node where required

How n8n IF node convert types where required?

You can convert types in the n8n IF node by coercing values with expressions or by converting earlier with a Set or Function node. This tutorial gives clear Docker and Node.js examples that turn strings into numbers and booleans so the IF node works correctly.

What You Need

  • n8n running in Docker or locally with Node.js.
  • A simple workflow with incoming JSON data.
  • Access to the n8n editor and a Function or Set node.

n8n IF node convert types where required: Why convert types

IF node comparisons depend on types. Strings that look like numbers do not behave like numbers. Converting early avoids wrong branches. Use Set or Function nodes to coerce types before the IF node.

Step 1: Install n8n with Docker

Start a quick n8n instance with Docker. This runs n8n on port 5678 and stores data locally.

docker run -it --rm -p 5678:5678 -v ~/.n8n:/home/node/.n8n n8nio/n8n

Open http://localhost:5678 and create a simple workflow.

Step 2: Convert types with a Set node

Use a Set node to create a typed field. Use an expression to parse numbers or booleans.

// In the Set node use the expression editor for a new field `amountNumber`:
{{$json["amount"] ? parseFloat($json["amount"]) : 0}}

This sets amountNumber as a real number. Now the IF node can compare as a number.

Step 3: Convert types with a Function node

Function node code gives more control for multiple fields. The Function node runs JavaScript on items.

return items.map(item => {
  // numeric conversion
  item.json.amount = Number(item.json.amount);
  // boolean conversion from strings like 'true' or 'false'
  item.json.active = item.json.active === 'true' || item.json.active === true;
  return item;
});

After this, the IF node reads item.json.amount as a number and item.json.active as a boolean.

Step 4: Use expressions inside the IF node

Set the IF node to use an expression that expects a number or boolean. Use an expression when you need inline coercion.

// Example boolean expression for the IF node
{{$json["amount"] > 100 && $json["active"] === true}}

This expression assumes amount is a number. If amount was a string, parse it first as shown above.

Step-by-step logic to implement

  • 1. Receive raw JSON input.
  • 2. Add a Set or Function node to coerce types.
  • 3. Verify converted fields in the node preview.
  • 4. Connect the IF node and use number or boolean comparisons.
  • 5. Send results to the next nodes based on IF branches.

Update

Update your workflow when input formats change. Add validation in the Function node. Keep conversion logic in one place for easier updates.

To update n8n Docker image:

docker pull n8nio/n8n
docker stop $(docker ps -q --filter ancestor=n8nio/n8n)
docker run -it --rm -p 5678:5678 -v ~/.n8n:/home/node/.n8n n8nio/n8n

Security

Do not run untrusted code inside Function nodes. Sanitize inputs before conversion. Use environment variables for secrets, not hard-coded values.

Limit access to the n8n editor and use strong credentials. Validate numeric ranges and reject malformed data early.

Done

You now know how to make the n8n IF node convert types where required. Use Set or Function nodes and expressions to ensure correct comparisons. Test with sample data and keep conversion centralized.


Neil
Written by Neil

Neil is a true n8n geek who lives and breathes workflow automation. He dives deep into nodes, triggers, webhooks, custom logic, and self-hosting setups, sharing everything he learns about n8n on AutomationCompare.com. As part of a broader team of automation specialists, Neil focuses purely on mastering n8n and helping others unlock its full potential.

Keep Reading

Scroll to Top