# If block matches string literal, but not string expression

**URL:** <https://community.n8n.io/t/if-block-matches-string-literal-but-not-string-expression/2484>\
**Category:** Questions\
**Created:** [September 29, 2020, 4:46am UTC](https://community.n8n.io/t/if-block-matches-string-literal-but-not-string-expression/2484 "2020-09-29T04:46:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trey](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/trey/32/1987_2.png) [@trey](https://community.n8n.io/u/trey)\
**Post date:** [September 29, 2020, 4:46am UTC](https://community.n8n.io/t/if-block-matches-string-literal-but-not-string-expression/2484/1 "2020-09-29T04:46:47Z")

</div>

Hi again! I’m still quite new here, but I was making good progress until I hit an issue with the If block that is driving me bonkers.

I’m trying to use an If block to match against a string evaluated from an expression. I can match data against a string literal without issue, but if I use an expression that produces the exact same string, it doesn’t match. What could I possibly be doing wrong here?

Thanks so much for your patience.

```auto
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n {\n json: {\n name: 'bar'\n }\n },\n {\n json: {\n name: 'foo'\n }\n }\n];"
      },
      "name": "Test Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n {\n json: {\n name: 'foo'\n }\n }\n];"
      },
      "name": "Search Value",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "foo"
            }
          ]
        }
      },
      "name": "Literal Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        400
      ],
      "notesInFlow": true,
      "notes": "Works"
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "={{$node[\"Search Value\"].json[\"name\"]}}"
            }
          ]
        }
      },
      "name": "Expression Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        200
      ],
      "notesInFlow": true,
      "notes": "Doesn't work"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Search Value",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test Data": {
      "main": [
        [
          {
            "node": "Literal Match",
            "type": "main",
            "index": 0
          },
          {
            "node": "Expression Match",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Value": {
      "main": [
        [
          {
            "node": "Test Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [September 29, 2020, 6:05am UTC](https://community.n8n.io/t/if-block-matches-string-literal-but-not-string-expression/2484/2 "2020-09-29T06:05:07Z")

</div>

As we do not like people to be driven bonkers I try my best to help you!

You are having this issue because you are comparing the data of two nodes that have a different number of items.

Search Value:

1. Item: { name: ‘foo’ '}

Test Data:

1. Item: { name: ‘bar’ '}
2. Item: { name: ‘foo’ '}

So in the IF-Node the following is happening because n8n automatically compares the items with the same index:

1. Item: It compares “foo” with “bar” =\> does not match so `false`
2. Item: It compares `undefined` with “foo” =\> does not match so `false`

Hope that helps!

---

<div class="post-metadata">

**Author:** ![trey](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/trey/32/1987_2.png) [@trey](https://community.n8n.io/u/trey)\
**Post date:** [September 29, 2020, 3:55pm UTC](https://community.n8n.io/t/if-block-matches-string-literal-but-not-string-expression/2484/3 "2020-09-29T15:55:38Z")

</div>

Thank you @jan! This makes sense. And if someone stumbles on this thread with the same problem/question, here is what I needed to use as an expression:

```auto
{{$item(0).$node["Search Value"].json["name"]}}
```

This pins the value received from the “Search Value” node at item 0 so it is always used as a comparator. Here’s the fixed workflow:

```auto
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n {\n json: {\n name: 'bar'\n }\n },\n {\n json: {\n name: 'foo'\n }\n }\n];"
      },
      "name": "Test Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n {\n json: {\n name: 'foo'\n }\n }\n];"
      },
      "name": "Search Value",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "foo"
            }
          ]
        }
      },
      "name": "Literal Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        400
      ],
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "={{$item(0).$node[\"Search Value\"].json[\"name\"]}}"
            }
          ]
        }
      },
      "name": "Expression Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        200
      ],
      "notesInFlow": true
    }
  ],
  "connections": {
    "Test Data": {
      "main": [
        [
          {
            "node": "Literal Match",
            "type": "main",
            "index": 0
          },
          {
            "node": "Expression Match",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Value": {
      "main": [
        [
          {
            "node": "Test Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
```
