skip to Main Content

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!

2

Answers


  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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search