How to correctly return a list of dictionaries in Zapier Code (Python)?

╄→尐↘猪︶ㄣ 提交于 2020-01-15 05:35:26

问题


The Zapier code documentation says that the output of a code zap can be either a dictionary or a list of dictionaries (See "Data Variable" section: https://zapier.com/help/code-python/).

When doing this,

output = [{'Booking':'Shirt'},{'Booking':'Jeans'}]

the output of the code returns only the first dictionary, however:

runtime_meta__duration_ms:  2
runtime_meta__memory_used_mb:   22
id: [redacted]
Booking:    Shirt
Fields with no value:
runtime_meta__logs

What am I doing wrong here? Thanks a lot!


回答1:


David from the Zapier platform team here. Code steps returning an array is a mostly undocumented (because there's no UI support and it's confusing, as you can tell) feature.

When testing, it'll only show the first item in the array. When it runs for real, all steps after the code step will run for each item in the array. The task history will reflect this

So set up the zap and turn on and it'll work like you expect.

Sorry for the confusion and let me know if you have any other questions!




回答2:


For anyone still looking for an answer to this questions, below is what find out returning list in Zapier.

# first import and convert your input value to an array.
# special note any line items imported into a python variable are converted to list format.
my_items = input_data['my_CSV_string']
my_list_of_items = my_items.split(",")

# Create a new list array 
my_new_list = []

length = len(my_list_of_items)
#Do all your computations    
for i in range(length): 
    my_new_list.append(float(my_list_of_items[i])*1.5)

# After completing any tasks you can return the list as follows, 
# If you are using line items keep the list in its original format
return {
    'my_processed_values': my_new_list,
    'original_values': my_list_of_items
}

# If you want to return it as a CSV "basically making the array flat"
my_old_CSV_list= ','.join(map(str, my_list_of_items))
my_new_CSV_list= ','.join(map(str, my_new_list))
return {
    'my_processed_cvs_values': my_new_CSV_list,
    'original_values': my_list_of_items
}

Hope this helps. I am not a Python expert but in theory the more lists used the longer the zap will take to process. Try to keep your python processing time to the lowest.

Best,



来源:https://stackoverflow.com/questions/47364837/how-to-correctly-return-a-list-of-dictionaries-in-zapier-code-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!