Guest User

Untitled

a guest
Jan 18th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /*
  2. EXAMPLE:
  3. 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.'
  4. */
  5.  
  6.  
  7. function extract_piece_of_data(entire_string_of_text, characters_before_desired_data, characters_after_desired_data) {
  8.  
  9. 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);
  10.  
  11. var desired_data = desired_data_with_stuff_after_it.substr(0, desired_data_with_stuff_after_it.indexOf(characters_after_desired_data));
  12.  
  13. return desired_data;
  14.  
  15. }
  16.  
  17.  
  18. // Get the messy data string from Zapier's built-in "input" variable.
  19. // var messy_data = input.messy_data_from_previous_trigger_or_action;
  20.  
  21.  
  22. // Remember to escape any double quotes in your function arguments with the backslash (\) character
  23. var service = extract_piece_of_data(inputData.body, "Service:", "Time:");
  24. var event = extract_piece_of_data(inputData.body, "Event:", "AccountId:");
  25. var time = extract_piece_of_data(inputData.body, "Time: ", "RequestId:");
  26. var cause = extract_piece_of_data(inputData.body, "Cause: ", "StartTime:");
  27. var instance_id = extract_piece_of_data(inputData.body, "EC2InstanceId: ", "Details:");
  28. var customer = extract_piece_of_data(inputData.customer, "cloud.", "@mediatemple.net");
  29. // var name = extract_piece_of_data(messy_data, "\"name\": \"", "\"} Sometimes");
  30. // var email = extract_piece_of_data(messy_data, "\"email\": \"", "\"} Thankfully");
  31.  
  32.  
  33. // 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.
  34. console.log(inputData.customer);
  35. output = {aws_service: service, aws_event: event, time: time, cause: cause, instance_id: instance_id, customer: customer};
Add Comment
Please, Sign In to add comment