1

I have a form that is returning a string of visitors names. Returned string looks like this:

[{"First Name":"JAMES","Last Name":"SMITH"},{"First Name":"SARAH","Last Name":"SMITH"}] 

I'm using JavaScript in Zapier to remove the special characters from the string:

let INPUT = inputData.INPUT; let OUTPUT = INPUT.replace(/[^a-z0-9]/gi, ''); output = [{INPUT, OUTPUT}]; 

but that gives me this output: FirstNameJAMESLastNameSMITHFirstNameSARAHLastNameSMITH

My ultimate goal is to produce something like this: JAMES SMITH SARAH SMITH etc

Is it possible to remove the special characters EXCEPT for the string },{? I could then use zapier to split the records using },{ as the identifier.

Thanks!

3
  • You essentially have an array of objects there. Look into how to parse the object values. It's well covered on SO (1, 2). Manipulating the string isn't really the right way to do this. Commented Aug 22, 2023 at 16:09
  • @jackstrum That is JavaScript.... Commented Aug 22, 2023 at 17:27
  • @jackstrum The code I posted is JavaScript. Commented Aug 22, 2023 at 18:30

2 Answers 2

1

I am not terribly familiar with Zapier, but assuming it has basic JavaScript functionality I would be inclined to convert the string to an object and work from there rather than parse it myself or use regular expressions. You can do:

let parsedString = JSON.parse(INPUT) 

Which will give you an object instead of a string. Much easier to work with, and you can loop through it to perform any operations you need to on each visitor:

let parsedString = JSON.parse('[{"First Name":"JAMES","Last Name":"SMITH"},{"First Name":"SARAH","Last Name":"SMITH"}]') parsedString.forEach(a => { console.log(`${a["First Name"]} ${a["Last Name"]}`) })

If you only need a string of visitor names separated by a space and don't need to perform any actions on individual visitors, you can do:

JSON.parse(str).flatMap(Object.values).join(' ') 

 console.log( JSON.parse('[{"First Name":"JAMES","Last Name":"SMITH"},{"First Name":"SARAH","Last Name":"SMITH"}]').flatMap(Object.values).join(' ') )

As pointed out by Unmitigated

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this is really useful. I tried the code code JSON.parse(str).flatMap(Object.values).join(' ') 'code' and received the error ReferenceError:str is not defined I believe I need to map the output value from the form to this in some way?
@jackstrum "str" is a common abbreviation for "string" and is just a placeholder like "John Doe" which you would replace with the name of the variable containing your string. It seems like you're storing the string you want to parse in a variable called "INPUT" so you would use JSON.parse(INPUT).flatMap(Object.values).join(' ') in your case.
-1

How are you? Here is the code snippet, I just made, according to you requirement.

const data = [ { "First Name": "JAMES", "Last Name": "SMITH" }, { "First Name": "SARAH", "Last Name": "SMITH" }, ]; const names = data.map((item) => Object.values(item) // only extract values not the keys from object. ["JAMES", "SMITH"] for first item in array. .map((e) => e.replace(/[^a-z0-9]/gi, "")) .join(" ") ); console.log(names); // names // [ 'JAMES SMITH', 'SARAH SMITH' ]

I hope this code works for you. Thank you.

2 Comments

thanks! unfortunately the form will always return different visitors names, so I need the code to run on whatever the form returns, so I assume the code cannot contain actual names or it will run unless the names match those in the code lol.
Can you give me, how the form returns in detail? Then I can make another code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.