Split 'Full Name' with 'Code' integration

Describe the issue/error/question

How to split a ‘Full Name’ that comes through a Webhook into ‘First Name’ ‘Last Name’ which will be used separately? I know 0 about JavaScript and here is the JSON I need to work with:

[
  {
    "params": {},
    "query": {},
    "body": [
      {
        "object": {
            "id": 1234567,
          },
          "user": {
            "name": "Ben Benson",
          },
        }
      }
    ]

How do I change "name": "Ben Benson" to this:

[
  {
    "params": {},
    "query": {},
    "body": [
      {
        "object": {
            "id": 1234567,
          },
          "user": {
            "fname": "Ben",
            "lname": "Beson",
          },
        }
      }
    ]

Here is the workflow: I receive “Full name” (in one line) from webhook, and I need the Code to split it into two first and last name objects.

Any help will be greatly appriciated!

I’m using something like:

{{ $node[“Webhook”].json[“body”][“object”][“user”][“name”] .split(" ")[0]}}

and

{{ $node[“Webhook”].json[“body”][“object”][“user”][“name”] .split(" ")[1]}}

To get the two names - .split(" “)[0] will get the text before the space and .split(” ")[1] will get it after

Hi @bigbeka

Welcome to the community!
@seank1968 gave the easiest way to do it.
But you will run into trouble with names that contain more than 2 words.
Using split and then using something like .slice(1).join(" ") for the second one would allow you to have all parts of the last name in the name field.
Hope that makes sense.

@BramKn @seank1968 Many thanks for your replies! :pray:

I tried pasting the suggested code (from @seank1968) into the Code box, but no luck. As mentioned, I have zero experience with JavaScript!

Not in a code node - you can use it diectly in the Mailchimp node in the first and last name fields

@BramKn is there a way of specifying the ‘last’ element of a split?

@seank1968 Yes. the last element you can get by .split().length - 1

@seank1968 Many thanks, it sort of works now. As a test I am sending the below strings to my Telegram:

First: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[0]}}
Last: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .slice(1).join(" ")[1]}}

I am able to get the First Name, but the second part is empty.

@BramKn, Yes, I would want to get the first name in the FNAME and consequent names in the LNAME.

Any idea why .slice(1).join(" ")[1] would not work?

Hi @bigbeka

The index 1 doesnt exist probably.
remove that [1] in the end.

Hi @BramKn, thank you for your persistent help!

No luck with removing the [1]
This is what I tried just now:
First: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[0]}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .slice(1).join(" ")}}

There seems to be something about .slice(1).join(" ")

I have also tried with just ‘FNAME LNAME’ when triggering the workflow, no luck.

Attaching the screenshots of the workflow executions:


You also need the split before the slice

@BramKn No luck. I have tried the following variations (First name works fine, so left is unchanged):
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[0].slice(1).join(" ")}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[1].slice(1).join(" ")}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ").slice(1).join(" ")}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split[0].slice(1).join(" ")}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split[1].slice(1).join(" ")}}
Last: {{ $item("1").$node["Webhook"].json["body"]["0"]["object"]["name"] .split.slice(1).join(" ")}}

@BramKn & @seank1968 Thank you for your help!
The script you provided has solved my issue and this is what is working now:
It was self imposed issue that caused the problem: $item("0") I had typed ("1") instead of ("0")

The following script is working:
First: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[0]}}
Last: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ").slice(1).join(" ")}}

Here is the output:

1 Like

hehe you are going crazy with the different options. I do see a space before the .split() which I am not sure of, that that doesn’t break it.
Also not sure why u are grabbing item 1 instead of item 0?
this should work if I am looking at it correctly:

{{ $item(“0”).$node[“Webhook”].json[“body”][“0”][“object”][“name”].split(" “).slice(1).join(” ")}}

ah so you beat me to it. nice that it works!

@BramKn Many many thanks!

I confirm that removing space after [name] also works:
{{ $item(“0”).$node[“Webhook”].json[“body”][“0”][“object”][“name”].split(" “).slice(1).join(” ")}}

I hope this can help others who are trying to split a string of output data into multiple fields.

But I feel like n8n is missing split filter (there is merge) and Split in Batches is slightly different I think.

1 Like

the split in batches and merge and such are for the workflow to manage the path it is taking. Has nothing to do with transforming the data. There is however a community node that has some tranformation options.

Thank you, I will check the community nodes out, but DIYing still feels good.

1 Like

@BramKn Could I please pick your brain on this topic a bit longer?

I have pushed the following 2 scripts to production, and of course there was an issue in one of the triggers. User entered just one Name and no other names to the field. The service that is triggering the webhook collect just “Name” field.

How to make the second (Last name) script optional?

First name: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"] .split(" ")[0]}}
Last name: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"].split(" ").slice(1).join(" ")}}

Mailchimp node does not want to process the trigger without having all name fields filled.

EDIT: I could probably place IF node between Webhook and Mailchimp nodes and check the trigger before it hits the Mailchimp node and direct the trigger accordingly to a Mailchimp node that is with full name configured, or Mailchimp node that is just with First name. This seems overkill to me.

I think you can use

|| ’ ’

in the end so:
Last name: {{ $item("0").$node["Webhook"].json["body"]["0"]["object"]["name"].split(" ").slice(1).join(" ")|| ' '}}

1 Like