Extract first and last name from a full name string

I am polling customer data from my CRM using an API. The endpoint returns a single field with the fill name of customer, and I need send to split this into first and last name.

For instance, aceptable result would beme

“full name”: “Wesley” => “first name”: “Wesley”, “last name”: “”
“full name”: “Fernando Pereira” => “first name”: “Fernando”, "last name :“Pereira”
“full name”: “Joao Pedro da Silva” => “first name”: “Joao”, "last name :“Silva”

I don’t know JS. Can someone help?

Information on your n8n setup

  • n8n version: 0.204.0
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: desktop for Mac

Hi @fxholl,
you can use expressions to achieve your result, but a little bit of JS will be part of the solution since everything inside expressions is JS.

I created a sample workflow to illustrate.

Thank you! I’ll be using that expression.

When there is only one name, say “John”, the output is “John John”. Is there a simple way to avoid that?

Hi @fxholl, I just had a look at the example and the output appears correct for me. @marcus’ example even includes the “one name only” case:

Perhaps you can provide an example of the JSON input causing trouble for you?

1 Like

my mistake!

Np at all, there’s always a chance we miss something :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.