IF Node: How to Handle if the JSON doesn't contain a specific key and value

Let’s say If the returning JSON output has the key and value.
Example: last_name: Naveen

How to handle if the JSON doesn’t contain those? Means what to do if there is not last_name in JSON how to handle that?

Anything can be done with the help of Function Node?

Selection_294

@mcnaveen Not sure if I understood well, but if I did, the example below should do it.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      firstname: 'ricardo',\n      lastname: 'espinoza'\n    }\n  },\n  {\n  json: {\n      firstname: 'ricardo',\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        470,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"Function\"].json[\"lastname\"] ? true : false}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        670,
        300
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        910,
        180
      ],
      "notesInFlow": true,
      "notes": "lastname exist"
    },
    {
      "parameters": {},
      "name": "NoOp1",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        910,
        410
      ],
      "notesInFlow": true,
      "notes": "lastname does not exist"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3 Likes

This works great. Thanks @RicardoE105

We did mistake in expression. Is there any guide for that?

There is a bit on the docs here. The key is that everything between {{...}}) that can execute JavaScript code, which offers access to special variables to access data.

1 Like

Awesome Thanks.