Split first name and second name of a form field

Hey guys,

I am migrating from integromat to n8n and need to split the first name and second name of a form received via webhook and capitalize the words.

Any ideas how I can do this on n8n?

Thanks in advance

Note: In integromat the solution I use is shown below:

Screen Shot 2021-06-14 at 3.23.20

Hey @bbarbi!

Welcome to the community :sparkling_heart:

Can you share the output that you receive? If you’re familiar with JavaScript, you can use the Function node and write custom code to do this :slight_smile:

1 Like

Thank you very much Harshil :raised_hands:

I was already considering that function node would be the best solution for this problem. Unfortunately my javascript knowledge is very basic. I will study about it. :+1:

1 Like

No worries :slight_smile:

If you face any issues, or need any help, feel free to post it here :slight_smile:

1 Like

I don’t know if it’s correct, but it worked. :raised_hands: :raised_hands: :raised_hands:

Learning code little by little. :heart:

N8N is awesome!

Screen Shot 2021-06-14 at 6.58.02

2 Likes

Yaay! This is amazing. I am so happy that you’re learning to code as well :wink:

FYI. If it is a smaller code snippet and you are referencing it once you can use it in the Expression editor as well. Below is an example that will help you understand it better

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "full name",
              "value": "Harshil Agrawal"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Name",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        230
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "First Name",
              "value": "={{$json[\"full name\"].split(\" \")[0]}}"
            },
            {
              "name": "Last Name",
              "value": "={{$json[\"full name\"].split(\" \")[1]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Split name",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        700,
        230
      ]
    }
  ],
  "connections": {
    "Set Name": {
      "main": [
        [
          {
            "node": "Split name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The first Set node (Set name) is creating mock data. The second Set node (Split name), uses the .split() method and references the first and last name.

I hope this helps :slight_smile:

1 Like

Yes, it helps a lot. :heart:

Thank you for your help @harshil1712 :facepunch:

1 Like

Hello @bbarbi another approach is to using directly in the expression like this in cases when you have more than two words in the value of name.

firstname = {{$json[“Name”].split(" “)[0]}}
lastname = {{$json[“Name”].split(” ").slice(1).join(’ ')}}

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Name",
              "value": "John Taylor White"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        559,
        277
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "firstname",
              "value": "={{$json[\"Name\"].split(\" \")[0]}}"
            },
            {
              "name": "lastname",
              "value": "={{$json[\"Name\"].split(\" \").slice(1).join(' ')}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        759,
        277
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
5 Likes

Thank you @rodrigoscdc :facepunch: :+1: