How to parse data with JavaScript Code step using .split()

  • 15 February 2022
  • 15 replies
  • 2966 views
How to parse data with JavaScript Code step using .split()
Userlevel 7
Badge +14

📌  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()[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 NameFirst = Content.split("First Name:")[1].split("\n")[0].trim();
let NameLast = Content.split("Last Name:")[1].split("\n")[0].trim();
let Email = Content.split("Email:")[1].split("\n")[0].trim();
let Phone = Content.split("Phone:")[1].split("\n")[0].trim();

output = [{NameFirst, NameLast, Email, Phone, Content}];

 

The Results

Contribution by Troy Tessalone

Troy is a Certified Zapier Expert who automates workflows with no-code and low-code apps to help clients save time and make money.


15 replies

Userlevel 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?

Userlevel 1

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()[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 Date = Content.split("Date/Time:")[1].split("\n")[0].trim(); 

let Location = Content.split("Location:")[1].split("\n")[0].trim(); 

let Address = Content.split("Address:")[1].split("\n")[0].trim(); 

let Incident = Content.split("Incident:")[1].split("\n")[0].trim(); 

let IncidentDetails =Content.split("Incident Details:")[1].split("\n")[0].trim(); 

output = [{Date, Location, Address, Incident, IncidentDetails}];

 

Results:

 

Date: 09/29/22 @ 11:16

Location: Phoenix, AZ (Maricopa County)

Address: 6236 N Black Canyon Access Rd

Incident: 1 Alarm Fire

IncidentDetails: U/D: E918-CMD o/s 3-story apt. W/F

 

Userlevel 7
Badge +14

@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()[0] returns the segment before the split value 

// .split()[1] 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()[0] returns the segment before the split value 

// .split()[1] 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>

Chapter: Arizona

Date/Time: 09/29/22 @ 11:16

Location: Phoenix, AZ (Maricopa County)

Address: 6236 N Black Canyon Access Rd

Incident: 1 Alarm Fire

Incident Details: U/D: E918-CMD o/s 3-story apt. W/F

on 2nd floor. CMD requests full

First Alarm balance.

(c) 2022 911 iMedia, Inc.

All Rights Resvered

 

Results:

Date :09/29/22

Address: 6236 N Black Canyon Access Rd

City: Phoenix

State: AZ

Incident:1 Alarm Fire

IncidentDetails: U/D: E918-CMD o/s 3-story apt. W/F

id: skCGR0VLZ3zFujDHv6Sdc1VgtIakbRGe

runtime_meta

memory_used_mb

62

duration_ms

5

logs

async

false

Userlevel 7
Badge +14

@PeterQuinn 

That’s because you are splitting at the end of a line.

You’ll need to adjust this:

let IncidentDetails =Content.split("Incident Details:")[1].split("\n")[0].trim(); 

Hi @Troy Tessalone 

That’s what I have in my code, but still cuts it off.

 

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 DateTime = Content.split("Date/Time:")[1].split("\n")[0].trim();

let Date = DateTime.split("@")[0].split("\n")[0].trim();

let Location = Content.split("Location:")[1].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:")[1].split("\n")[0].trim();

let Incident = Content.split("Incident:")[1].split("\n")[0].trim();

let IncidentDetails = Content.split("Incident Details:")[1].split("\n")[0].trim();

output = [{Date, Address, City, State, Incident, IncidentDetails}];

 

Userlevel 7
Badge +14

@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

Userlevel 7
Badge +14

Hi @starhouse 

Good question.

You may be able to simply use Formatter > Text > Split to get the desired data point.

Userlevel 1

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.

 

Userlevel 7
Badge +14

@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:

 

Userlevel 1

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:")[1].split("\n")[0].trim(); l
et Phone = Content.split("Phone:")[1].split("\n")[0].trim(); 
let FirstName = Content.split("FirstName:")[1].split("\n")[0].trim(); 
let LastName = Content.split("LastName:")[1].split("\n")[0].trim(); 
output = [{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

Userlevel 1
Badge

Fantastic walkthrough. Thank you for this.

Reply