Why am I getting a Runtime.MarshalError when using this code in Zapier?

寵の児 提交于 2019-12-11 04:28:47

问题


The following code is giving me:

Runtime.MarshalError: Unable to marshal response: {'Yes'} is not JSON serializable

from calendar import monthrange

def time_remaining_less_than_fourteen(year, month, day):
    a_year = int(input['year'])
    b_month = int(input['month'])
    c_day = int(input['day'])
    days_in_month = monthrange(int(a_year), int(b_month))[1]
    time_remaining = ""

    if (days_in_month - c_day) < 14:
        time_remaining = "No"
        return time_remaining

    else:
        time_remaining = "Yes"
        return time_remaining


output = {time_remaining_less_than_fourteen((input['year']), (input['month']), (input['day']))}

#print(output)

When I remove {...} it then throws: 'unicode' object has no attribute 'copy'


回答1:


David here, from the Zapier Platform team.

Per the docs:

output: A dictionary or list of dictionaries that will be the "return value" of this code. You can explicitly return early if you like. This must be JSON serializable!

In your case, output is a set:

>>> output = {'Yes'}
>>> type(output)
<class 'set'>
>>> json.dumps(output)
Object of type set is not JSON serializable

To be serializable, you need a dict (which has keys and values). Change your last line to include a key and it'll work like you expect:

#         \ here /
output = {'result': time_remaining_less_than_fourteen((input['year']), (input['month']), (input['day']))}


来源:https://stackoverflow.com/questions/54407298/why-am-i-getting-a-runtime-marshalerror-when-using-this-code-in-zapier

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