Skip to content

Instantly share code, notes, and snippets.

@reganstarr
Last active December 1, 2021 13:06
Show Gist options
  • Save reganstarr/f6e946214a0ff70d5e95 to your computer and use it in GitHub Desktop.
Save reganstarr/f6e946214a0ff70d5e95 to your computer and use it in GitHub Desktop.
/*
EXAMPLE:
input.messy_data_from_previous_trigger_or_action = 'This is a single string of text that contains lots of different data. {"name": "John"} Sometimes when you are building a Zap, you will need to pull out one piece of data from a long, messy string of text. {"email": "johnsmith@gmail.com"} Thankfully, you can use the "Formatter by Zapier" app or the "Code by Zapier" app to extract the information you need.'
*/
function extract_piece_of_data(entire_string_of_text, characters_before_desired_data, characters_after_desired_data) {
var desired_data_with_stuff_after_it = entire_string_of_text.substr(entire_string_of_text.indexOf(characters_before_desired_data) + characters_before_desired_data.length);
var desired_data = desired_data_with_stuff_after_it.substr(0, desired_data_with_stuff_after_it.indexOf(characters_after_desired_data));
return desired_data;
}
// Get the messy data string from Zapier's built-in "input" variable.
var messy_data = input.messy_data_from_previous_trigger_or_action;
// Remember to escape any double quotes in your function arguments with the backslash (\) character
var name = extract_piece_of_data(messy_data, "\"name\": \"", "\"} Sometimes");
var email = extract_piece_of_data(messy_data, "\"email\": \"", "\"} Thankfully");
// Output the data as an object called "output". The object's keys can be whatever you want. In this example, I chose to use the keys first_name and email_address.
output = {first_name: name, email_address: email};
@tudormateescu
Copy link

Bargle. We hit an error creating a run javascript. :-( Error:
TypeError: Cannot read property 'substr' of undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment