How do I convert a specific string value to true / false?

Hey!
How do I convert a specific string value to true / false and apply to a Boolean object in a set node?


Hi @Roket

I made a simple workflow based on my understanding. Not sure this is right or wrong.

image

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        560,
        430
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Yes",
              "value": "This is True"
            },
            {
              "name": "No",
              "value": "This is False"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        740,
        430
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"Yes\"]}}",
              "operation": "contains",
              "value2": "True"
            },
            {
              "value1": "={{$json[\"No\"]}}",
              "operation": "contains",
              "value2": "True"
            }
          ]
        },
        "combineOperation": "any"
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        890,
        430
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "True",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1080,
        360
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "False",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "name": "Set3",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1080,
        500
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

mcnaveen, thanks for the answer!
This is not what I want (.
I’ll try to explain in words.

  1. I have a webhook with a payload that contains multiple strings with a “Y” or “N” value.
  2. Next, I have a set node, in which I want to convert this value to true or false, respectively, but changing the type of the object (if I correctly interpret the terminology, perhaps this is a misunderstanding of my task) from string to boolean.
  3. The expression I need to write inside the boolean field, which is located in the set node.

    I tried for a few hours to figure out the javascript here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
    But nothing happened, unfortunately (.

Hey @Roket, since you are using the assignment operator = instead of the comparison operator in JavaScript ===, the is_need_corrector variable always gets the value Y assigned and it will always return true. Since you want conversion to boolean values here, you don’t really need to use the conditional operator as the comparison operator returns boolean values. More details here.

Here’s an example workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json: {is_need_corrector: 'Y'}}, {json: {is_need_corrector: 'N'}}, {json: {is_need_corrector: 'N'}}, {json: {is_need_corrector: 'Y'}}];"
      },
      "name": "Mock data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "is_need_corrector",
              "value": "={{$node[\"Mock data\"].json[\"is_need_corrector\"] ==='Y'}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Mock data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock data": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope that helps :slight_smile:

2 Likes

tanay, Thanks!
Its works!)

1 Like

That’s great to hear, have fun! :slight_smile:

1 Like