How to check for empty node output

Hi, I’m trying to use an IF node to detect empty output from a node, but I’m unable to get anything but true from it - how can I test for empty values coming out of a node?

First of all you have to know that n8n does not advance the chain if no data is available.
To avoid this, there is this setting in the respective node:
grafik
Than you can use a function to check if items are empty with:

items.length == 1 && Object.keys(items[0].json).length == 0

Here a complete example:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [];"
      },
      "name": "Always Output Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "functionCode": "return [{json: {empty: items.length == 1 && Object.keys(items[0].json).length == 0}}];"
      },
      "name": "Is Empty?",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json[\"empty\"]}}",
              "value2": true
            }
          ],
          "number": []
        }
      },
      "name": "IF Is Empty?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {},
      "name": "It was empty!",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1200,
        160
      ]
    },
    {
      "parameters": {},
      "name": "It was not empty!",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1200,
        440
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Always Output Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Always Output Data": {
      "main": [
        [
          {
            "node": "Is Empty?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Empty?": {
      "main": [
        [
          {
            "node": "IF Is Empty?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Is Empty?": {
      "main": [
        [
          {
            "node": "It was empty!",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "It was not empty!",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3 Likes