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!

isherwood
  • 58,414
  • 16
  • 114
  • 157
jackstrum
  • 31
  • 3
  • You essentially have an array of objects there. Look into how to parse the object values. It's well covered on SO ([1](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array), [2](https://stackoverflow.com/questions/55041867/convert-an-array-of-objects-to-array-of-the-objects-values)). Manipulating the string isn't really the right way to do this. – isherwood Aug 22 '23 at 16:09
  • @jackstrum That is JavaScript.... – epascarello Aug 22 '23 at 17:27
  • @jackstrum The code I posted _is_ JavaScript. – Unmitigated Aug 22 '23 at 18:30

2 Answers2

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

Shades
  • 667
  • 1
  • 7
  • 22
  • 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 Aug 22 '23 at 16:19
  • @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. – Shades Aug 22 '23 at 17:17
-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.

  • 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. – jackstrum Aug 22 '23 at 16:24
  • Can you give me, how the form returns in detail? Then I can make another code. – Eagering Dev Aug 22 '23 at 23:34