Switch Node Not Passing All Input Items

Describe the problem/error/question

Switch node configured for rules is switching based on values in item 1, but I want the output to retain both item 1 and 2 and only item 1 is being passed.

Please share your workflow

{
  "meta": {
    "instanceId": "545c531a0d167fb52e16d680acf7ab3bbf63b448d1250afffc090d6b18434529"
  },
  "nodes": [
    {
      "parameters": {
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "fabricApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
            },
            {
              "name": "token",
            }
          ]
        },
        "options": {}
      },
      "id": "b6843e2c-58d1-4d03-aadc-2dc65dc5f7d2",
      "name": "Fabric Search for Record",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1500,
        -260
      ],
      "alwaysOutputData": true,
      "credentials": {
        "fabricApi": {
          "id": "4",
          "name": "Fabric QA"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.data.id }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "3de64bfc-8938-44f5-a36e-25716f5e59b9",
      "name": "If Fabric Record Exists",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1720,
        -260
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "fabricApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
            },
            {
              "name": "token",
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{ \"category\": \"Feature\", \"titles\": [ { \"type\": \"title\", \"title\": \"Test\", \"locale\": \"ENG\" } ] }",
        "options": {}
      },
      "id": "cbb1153a-17fb-4a37-92ae-955591b0452a",
      "name": "Fabric Update Record",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        2080,
        -360
      ],
      "credentials": {
        "fabricApi": {
          "id": "4",
          "name": "Fabric QA"
        }
      }
    },
    {
      "parameters": {
        "mode": "chooseBranch",
        "output": "input2"
      },
      "id": "6f18eace-e2a6-4ca4-a79b-bdb638a43b80",
      "name": "Merge2",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        2080,
        -100
      ]
    }
  ],
  "connections": {
    "Fabric Search for Record": {
      "main": [
        [
          {
            "node": "If Fabric Record Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Fabric Record Exists": {
      "main": [
        [
          {
            "node": "Fabric Update Record",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
    0.219.1
  • Database (default: SQLite):
    PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
    Docker
  • Operating system:

Hey @ryanflomenco,

Welcome to the community :raised_hands:

It is a bit hard to test your workflow as it looks like you have modified it which has broken it, It also looks like you might be using a custom node as well.

Are you able to provide an example using maybe a code or set node to reproduce the data structure from the HTTP Request nodes?

Thanks @Jon

Here’s a mock example:

Is there a way to tell n8n that I want all input items output when a rule is matched as opposed to just the input item that matches? It would seem that if only input items that are matched are passed to the output that the Switch Node is functioning more how I would expect a For Loop to function (e.g. for all input items loop through them and if they match various rules do different things).

I can’t seem to achieve the desired output of passing all input items to output with an If Node either:

In this case Input item 1 that matches the true condition is passed to the true branch and input item 2 is passed to the false branch.

A work around could be to put a Merge Node after the If statement as well that combines the true and false branches and only executes when the If statement is true:

However, this requires adding a lot of complex logic with If and Merge nodes that I would like to avoid.



Another finding I can’t make sense of:

If I use {{ $json.input1 }} I get a true branch and a false branch.

If I use {{ $input.all()[0].json.input1 }} I get 2 items in the true branch.

If I use {{ $input.first().json.input1 }} I get 2 items in the true branch.

Why is the behavior of $.json different than $input.all()[0]? I must have missed something in the documentation.

There is a lot to go through here, So for the first one with Switch node…

This is working as expected, You are checking {{ $json.input1 }} for value and outputting to 0 as the second item is input2 it is correctly not being passed through. In n8n we run most nodes for each input item so 2 items coming in would be 2 runs each evaluating the routing rules. if a value didn’t match your rule would you really still want it to be moved through that branch?

With the If node it is the same deal, The value of {{ $json.input1 }} is being checked which is empty for the second input so input 1 would be true and input 2 would be false.

I think the important part here is what are you actually trying to do? If you don’t really care about the routing it sounds like just not having the If or Switch node solves that problem.

With the next set…

{{ $input.last().json.input2 }} is using the last item from the input and checking the input2 field, No matter which item the if is running for the last item is always going to be the same and in the example will always evaluate to true. So lets say I have 2 data structures like you have here…

[
  {
    "input1": "value"
  },
  {
    "input2": "value"
  }
]

The last item here only has a field called input2, The node is going to run once for each item but that last item is always going to be the same which is why you have both items coming out.

Using the same data structure for {{ $input.all()[0].json.input1 }} this is going to use all the items at once but it is going to use the first item in the array because the index has been specified, This is always going to be the first item.

The last one with {{ $input.first().json.input1 }} is the same outcome as the previous one because you are using the first() option which is always going to use the first item from the input items.

We have some docs on the inputs items here: Built in methods and variables reference | n8n Docs and our data structure here: Data structure | n8n Docs

Now I have got this far I am wondering if what you actually want is to merge the items into one item rather than appending them, If this is the case try the Combine mode and set it to match on the fields you want to merge and it should sort that out.

Let me know if I have missed anything.

1 Like

Thanks @Jon!

After some more investigation the behavior I am after can be accomplished with the Item List / Concatenate Items / All Item Data (Into Single List) node prior to my Switch or If nodes.

2 Likes

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