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:
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.
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:
@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(" ")}}
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:
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.
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.
@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.