JSON key does not appear in variable selector of IF node

I have an input data that as an array for each item. The array may be empty.

[
  {
    json: {
      somearray: [],
    }
  },
  {
    json: {
      somearray: [1,2],
    }
  }
]

I’m trying to check if the array is empty or not. When I connect IF node and open variable selector, I cannot select the key of the array (“somearray” above) if the array of the first input data is empty. Is that a bug or expected behavior?

That does not happen when the array is not empty.
Also, if I set manually the expression of IF condition as “{{$json[“id”].length}}”, the workflow seems to work fine even when the array is not selectable in variable selector.

{
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "=null {{$json[\"id\"].length}}",
              "operation": "smallerEqual"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        290
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: [1],\n    }\n  },\n  {\n    json: {\n      id: [1,2],\n    }\n  }\n];\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        290
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "=null {{$json[\"id\"].length}}",
              "operation": "smallerEqual"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        290
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Welcome to the community. @Hitoshi_Obu

Just checked the workflow you shared and it seems it was not working cuz there is a null before the expression that should not have been there.

When I connect IF node and open variable selector, I cannot select the key of the array (“somearray” above) if the array of the first input data is empty. Is that a bug or expected behavior?

Not sure what you mean here. When you say the key you mean the index? If so, then it’s as expected behavior. Either way, check the example below.

{
      "nodes": [
        {
          "parameters": {},
          "name": "Start",
          "type": "n8n-nodes-base.start",
          "typeVersion": 1,
          "position": [
            250,
            330
          ]
        },
        {
          "parameters": {
            "functionCode": "return [\n  {\n    json: {\n      id: [],\n    }\n  },\n  {\n    json: {\n      id: [1,2],\n    }\n  }\n];\n\n"
          },
          "name": "Function",
          "type": "n8n-nodes-base.function",
          "typeVersion": 1,
          "position": [
            560,
            340
          ]
        },
        {
          "parameters": {
            "conditions": {
              "number": [
                {
                  "value1": "={{$node[\"Function\"].json[\"id\"].length}}",
                  "operation": "smallerEqual"
                }
              ]
            }
          },
          "name": "IF1",
          "type": "n8n-nodes-base.if",
          "typeVersion": 1,
          "position": [
            840,
            340
          ]
        },
        {
          "parameters": {},
          "name": "NoOp",
          "type": "n8n-nodes-base.noOp",
          "typeVersion": 1,
          "position": [
            1130,
            170
          ]
        },
        {
          "parameters": {},
          "name": "NoOp1",
          "type": "n8n-nodes-base.noOp",
          "typeVersion": 1,
          "position": [
            1170,
            450
          ]
        }
      ],
      "connections": {
        "Start": {
          "main": [
            [
              {
                "node": "Function",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Function": {
          "main": [
            [
              {
                "node": "IF1",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "IF1": {
          "main": [
            [
              {
                "node": "NoOp",
                "type": "main",
                "index": 0
              }
            ],
            [
              {
                "node": "NoOp1",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      }
    }