在 Zapier 中使用此代码时,为什么会出现 Runtime.MarshalError? [英] Why am I getting a Runtime.MarshalError when using this code in Zapier?

查看:34
本文介绍了在 Zapier 中使用此代码时,为什么会出现 Runtime.MarshalError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码给了我:

Runtime.MarshalError:无法编组响应:{'Yes'} 不是 JSON 可序列化的

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)

当我删除 {...} 时,它抛出:'unicode' object has no attribute 'copy'

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

推荐答案

我在使用 Kinesis Firehose 的 lambda 转换蓝图 kinesis-firehose-process-record-python 时遇到了这个问题,这导致我来到这里.因此,我将向在 lambda 出现问题时也发现此问题的任何人发布解决方案.

I encountered this issue when working with lambda transformation blueprint kinesis-firehose-process-record-python for Kinesis Firehose which led me here. Thus I will post a solution to anyone who also finds this questions when having issues with the lambda.

蓝图是:

from __future__ import print_function

import base64

print('Loading function')


def lambda_handler(event, context):
    output = []

    for record in event['records']:
        print(record['recordId'])
        payload = base64.b64decode(record['data'])

        # Do custom processing on the payload here

        output_record = {
            'recordId': record['recordId'],
            'result': 'Ok',
            'data': base64.b64encode(payload)
        }
        output.append(output_record)

    print('Successfully processed {} records.'.format(len(event['records'])))

    return {'records': output}

需要注意的是,AWS 提供的用于 Python 的 Firehose lambda 蓝图是针对 Python 2.7 的,它们不适用于 Python 3.原因是在 Python 3 中,字符串和字节数组是不同的.

The thing to note is that the Firehose lambda blueprints for python provided by AWS are for Python 2.7, and they don't work with Python 3. The reason is that in Python 3, strings and byte arrays are different.

使其与由 Python 3.x 运行时支持的 lambda 配合使用的关键更改是:

The key change to make it work with lambda powered by Python 3.x runtime was:

改变

'data': base64.b64encode(payload)

进入

'data': base64.b64encode(payload).decode("utf-8")

否则,由于无法使用从 base64.b64encode 返回的字节数组序列化 JSON,因此 lambda 会出错.

Otherwise, the lambda had an error due to inability to serialize JSON with byte array returned from base64.b64encode.

这篇关于在 Zapier 中使用此代码时,为什么会出现 Runtime.MarshalError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆