Check if JSON key is null in IF node

Hello,

Apologies for the likely beginner-level question.

I’m using a function to create a simple JSON object:

{
"notNull":"notNull",
"Null":null
}

I would like to create an IF node to check whether the “Null” key is actually null. I’ve seen other similar questions but they seem to be more focused on if the entire JSON object’s data is empty.

In practice, we will be receiving purchase information from a webhook, and sometimes we will receive a null value if an option is not selected, and I would like to perform a different flow in these instances.

Is there any way to check for null in the GUI? If not, what’s the proper way to go about this?

Thanks in advance. :slight_smile:

You would use an expression in the If node to check a specific property on the input data into the node. Here’s an example. Notice how the field in the condition is purple with a dashed border, that’s because I set to an expression. Click the field and you’ll see the expression editor and some possible intputs on the left side. You might need to execute the workflow first for the input data to show up as an option since by default the previous node will have no data.

{
  "nodes": [
    {
      "parameters": {},
      "name": "property is not null",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "property",
              "value": "null"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        2929,
        165
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "property",
              "value": "null"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        430,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"property\"]}}",
              "value2": "null"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        630,
        300
      ]
    },
    {
      "parameters": {},
      "name": "property is null",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        150
      ]
    },
    {
      "parameters": {},
      "name": "property is not null",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "property is null",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "property is not null",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks for the answer.

It looks like in this flow, you’re setting the property as a string containing “null”, right? In my case I would need something that can check if a key’s value is of the JSON data type null, and not a string.

See the “nullNull” key below:

image

Then in that case I believe you should be able to change the second value of the If node to also be an expression and input {{null}}. I changed the example to use a function item node to output {"property":null} and it seems to be working:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return {\"property\": null}"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "property",
              "value": "null"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        2929,
        165
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"property\"]}}",
              "value2": "={{null}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        630,
        300
      ]
    },
    {
      "parameters": {},
      "name": "property is null",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        150
      ]
    },
    {
      "parameters": {},
      "name": "property is not null",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "return {\"property\": null}"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "FunctionItem": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "property is null",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "property is not null",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3 Likes

Perfect! That was it. :slight_smile:

Thank you.