Zapier中使用JavaScript匹配并分组输入数据需求
Solution: Match Predefined Keys to Corresponding Values & Group Them
Got it, let's work through this together! The core task is to pair your predefined keys with their matching values (since keys and values are in one-to-one order), ignore any extra keys, and group all values by each predefined key. Here's a straightforward way to do this using Python (perfect for a Zapier Code step):
Step 1: Define Your Predefined Keys
First, let's list out the 7 keys we care about (we'll use this to filter out unwanted keys):
TARGET_KEYS = [ "Project Details", "Project Title", "Addons", "Upgrade", "Word Count", "Ebook Type", "Upload your file here" ]
Step 2: Process Input Data
We'll split your input key and value strings into lists, then map each key-value pair to our target keys:
# Your input data (replace with Zapier's input variables if needed) input_keys = "Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your file here,_builder_id,_master_builder,_builder_id,_builder_info,Ebook Type,Word Count,Upgrade,Addons,Project Title,Project Details,Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your file here,_builder_id" input_values = "Book Description 3,Book Title 3,Book Cover Design - $59.00,No Package,Standard 10K - $270.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828454/001_Ebook Standard 325x325.png~~start,start1526659928051,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png,start1526659928051,1,start1526659872230,Start~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~start,Technical Ebook,Technical 15K - $450.00,No Package,No Addons,Book Title 2,Book Description 2,Book 1 Description,Book 1 Title,No Addons,Essential Package - $79.00,Standard 20K - $540.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828458/001_Ebook Standard 20k 325x325.png~~start,start1526659838425,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png,start1526659838425" # Split strings into lists keys_list = input_keys.split(",") values_list = input_values.split(",") # Initialize a dictionary to hold grouped values for each target key grouped_data = {key: [] for key in TARGET_KEYS} # Iterate through each key-value pair for key, value in zip(keys_list, values_list): if key in grouped_data: grouped_data[key].append({key: value}) # Convert the grouped data into the format you want (list of lists) final_output = [grouped_data[key] for key in TARGET_KEYS]
Step 3: Check the Output
When you run this code, the final_output will match your expected format:
[ [{'Project Details': 'Book Description 3'}, {'Project Details': 'Book Description 2'}, {'Project Details': 'Book 1 Description'}], [{'Project Title': 'Book Title 3'}, {'Project Title': 'Book Title 2'}, {'Project Title': 'Book 1 Title'}], [{'Addons': 'Book Cover Design - $59.00'}, {'Addons': 'No Addons'}, {'Addons': 'No Addons'}], [{'Upgrade': 'No Package'}, {'Upgrade': 'No Package'}, {'Upgrade': 'Essential Package - $79.00'}], [{'Word Count': 'Standard 10K - $270.00'}, {'Word Count': 'Technical 15K - $450.00'}, {'Word Count': 'Standard 20K - $540.00'}], [{'Ebook Type': 'Standard Ebook'}, {'Ebook Type': 'Technical Ebook'}, {'Ebook Type': 'Standard Ebook'}], [{'Upload your file here': 'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png'}, {'Upload your file here': 'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png'}] ]
Notes for Zapier Usage
- In Zapier, replace the hardcoded
input_keysandinput_valueswithinput_data['keys']andinput_data['values'](adjust the variable names to match your Zap's input fields). - The output will be a list you can use in subsequent Zap steps, with each sublist containing all the matching entries for one of your predefined keys.
内容的提问来源于stack exchange,提问作者Fabian C
相关产品推荐
相关产品推荐

