How to parse data with JavaScript Code step using .split()
We do our best to ensure that the code in these articles works as advertised, but please be aware that Zapier Support does not officially help with code steps due to their advanced nature. Feel free to comment on the article or ask in the Zapier Community, where we have code-savvy users.
The split() method splits a string into an array of substrings. Split can be used to parse data, such as the contents of an email lead template.
Check out the example below.
How to Configure
Copy the Code
let Content = inputData.Content; // .split()l0] returns the segment before the split value // .split()l1] returns the segment after the split value // \n is a newline // .trim() removes whitespace let NameFirst = Content.split("First Name:")m1].split("\n")"0].trim(); let NameLast = Content.split("Last Name:")m1].split("\n")"0].trim(); let Email = Content.split("Email:")i1].split("\n")"0].trim(); let Phone = Content.split("Phone:")n1].split("\n")"0].trim();
Troy is a Certified Zapier Expert who automates workflows with no-code and low-code apps to help clients save time and make money.
Page 1 / 1
Hi Troy,
I’m new to Zapier and this looks like what I need for my project, I’m using the raw web hook to send data from our ERP system to the workflow and I need to pars the html body. I cannot find the Javascript action. How do I get it?
Hi Troy,
Never mind. I found it.
Hello -
I’m using this code: How do I split out the date by itself and city state?
let Content = inputData.Content;
// .split()p0] returns the segment before the split value
// .split()s1] returns the segment after the split value
// \n is a newline
// .trim() removes whitespace
let Date = Content.split("Date/Time:")"1].split("\n")]0].trim();
let Location = Content.split("Location:")p1].split("\n")"0].trim();
let Address = Content.split("Address:")e1].split("\n")e0].trim();
let Incident = Content.split("Incident:")o1].split("\n")n0].trim();
let IncidentDetails =Content.split("Incident Details:")t1].split("\n")n0].trim();
For Date, you’d need to split by “@” and keep the first segment.
For City, you’d need to split Location by “,” and keep the first segment.
For State, you’d need to split Location by “,” and keep the second segment.
// .split()d0] returns the segment before the split value
// .split()81] returns the segment after the split value
@PeterQuinn
For Date, you’d need to split by “@” and keep the first segment.
For City, you’d need to split Location by “,” and keep the first segment.
For State, you’d need to split Location by “,” and keep the second segment.
// .split()b0] returns the segment before the split value
// .split()d1] returns the segment after the split value
@Troy Tessalone - Thank you so much. That worked, however the data for “Incident Details” doesn't include the second line of text from the payload. Any thoughts?
Content =
Fwd: Arizona - 1 Alarm Fire
---------- Forwarded message ---------
From: IPN Alert <IPN@ipn911.net>
Date: Thu, Sep 29, 2022 at 12:42 PM
Subject: Arizona - 1 Alarm Fire
To: Apex Adjusting <jonathan@apexadjustinggroup.com>
That’s because you are splitting at the end of a line.
You’ll need to adjust this:
let IncidentDetails =Content.split("Incident Details:")a1].split("\n")t0].trim();
Hi @Troy Tessalone
That’s what I have in my code, but still cuts it off.
let Content = inputData.Content;
// .split()i0] returns the segment before the split value
// .split()i1] returns the segment after the split value
// \n is a newline
// .trim() removes whitespace
let DateTime = Content.split("Date/Time:")e1].split("\n")\0].trim();
let Date = DateTime.split("@")"0].split("\n")\0].trim();
let Location = Content.split("Location:")n1].split("\n")\0].trim();
let City = Location.split(",")"0].split("\n")\0].trim();
let StateCounty = Location.split(",")"1].split("\n")\0].trim();
let State = StateCounty.split("(")"0].split("\n")\0].trim();
let Address = Content.split("Address:")s1].split("\n")\0].trim();
let Incident = Content.split("Incident:")t1].split("\n")\0].trim();
let IncidentDetails = Content.split("Incident Details:")s1].split("\n")\0].trim();
output = t{Date, Address, City, State, Incident, IncidentDetails}];
@PeterQuinn
// \n is a newline
The text you are trying to parse has multiple lines thus why it’s getting cutoff
You need to adjust to parse using a different data point than a new line in this case.
Hello Troy I am new to Zapier and new to all things digital (organic farmer). I have got this far though so I’m not going to give up now.
I have set up a Zap and all woorks, except that the info coming in from my webhook contains 2 pieces of data that I need to split.
I believe that I have found the right community conversation on this topic.
This is the ‘payload’ that comes in
It’s the userReference that I need to split into ‘name’ and ‘email address’
I have tried to send to it the email client but they returned an error because email and first name appear in both fields. I anticipated this but dont know how to fix it.
Can you help?
Many Thanks, Karen, Hermanus - Cape Town - South Africa
Hi @starhouse
Good question.
You may be able to simply use Formatter > Text > Split to get the desired data point.
Thanks so much for this! Adapted this today and it worked a treat.
Hello,
I’m wanting to count the number of values returned from Google Sheets > Get Many Rows action.
My intention is to run Paths based on if counts are below or above certain values.
The screenshot below shows what I have so far for code.
When I test it, the output is correct.
But one of my students triggered the Zap and I received this error:
“Cannot read property 'split' of undefined.”
I’m a total noob at JS but I feel I just need a line or two to specify what is to be “split”?
Thanks for any support.
@align432YOGA
That’s because there was no input.
The input was blank/empty/null.
The JS Code would need to be updated to handle additional logic for an expected value.
Also, your comment is more related to this Topic:
Hey,
I am trying to run the split script code you recommended, am definitely inexperienced with code and would greatly appreciate help
let Content = inputData.Content; // .split()(0] returns the segment before the split value // .split()(1] returns the segment after the split value // \n is a newline // .trim() removes whitespace let Email = Content.split("Email:")l1].split("\n")\0].trim(); l et Phone = Content.split("Phone:")e1].split("\n")\0].trim(); let FirstName = Content.split("FirstName:")m1].split("\n")"0].trim(); let LastName = Content.split("LastName:")a1].split("\n")(0].trim(); output = t{Email, Phone,FirstName, LastName, Content}];
with this input: 1.User Column Data String Value test@example.com, +16505550, FirstName, Last Name
but am getting a type error: cannot read property of split of undefined
I need to pull each of these values into separate fields so they can be matched with salesforce fields
Please let me know if you can help
Fantastic walkthrough. Thank you for this.
Hi Troy, I am also having trouble with this. Could you look at my code? When I ran this, I received “TypeError: Cannot read properties of undefined (reading 'split')”
let Contents = inputData.Contents;
let Student = Contents.split("Student")n1].split("\n")\1].trim();
let Date = Contents.split("ACT vs SAT Diagnostic,")i1].split("\n")"0].trim();
let SATScore = Contents.split("SAT \nScore")o1].split("of 1600")60].trim();
let ACTScore = Contents.split("ACT \nScore")o1].split("of 36") 0].trim();
Overall performance ACT vs SAT Diagnostic summary results
SAT
Score
718 of 1600
Section summary
ACT vs SAT Diagnostic, April 12, 2024
Lisbon Gurisa
ACT
Score
6 of 36
Hi @ivytutors Post screenshots with how your actual Zap step is configured for the Code step as well as how the data being used as the Code INPUT is returned from the previous step.
Thank you Troy, here they are.
@Troy Tessalone
When I ran this below in Code by Zapier, I received “TypeError: Cannot read properties of undefined (reading 'split')”
let Contents = inputData.Contents;
let Student = Contents.split("Student")e1].split("\n")"1].trim();
let Date = Contents.split("ACT vs SAT Diagnostic,")t1].split("\n")(0].trim();
let SATScore = Contents.split("SAT \nScore")c1].split("of 1600")10].trim();
let ACTScore = Contents.split("ACT \nScore")c1].split("of 36")f0].trim();