我有一个触发 Lambda 的 AWS Stepfunction。不幸的是,我无法正确解码输入:从 AWS Stepfunction 到 Lambda 的输入:{ "MessageNumber": 0, "message": { "MessageId": "ae5b5d59-6829-4a19-b508-2ba35614e2de", "ReceiptHandle": "AQEBJsP+Et+WcgstIR9kkA3sH4UDoxYYcoDT2YYa30K87Rx2wSEFaZNg3CNdsjE18LSDMNXWg7VoV/s/p0bSL0PaUxQ0FV34RAwavG0iGMi", "MD5OfBody": "a726ec7fdda41e454850580e1a2cf4b4", "Body": "{'stage': 'tst', 'directory': '\\\\\\\\mydomain\\\\data\\\\results', 'variableFilters': ['\\\\filters\\\\x1.txt', '\\\\filters\\\\a1.txt']}", "Attributes": { "SenderId": "xxx", "ApproximateFirstReceiveTimestamp": "xxx", "ApproximateReceiveCount": "2", "SentTimestamp": "xxx", "SequenceNumber": "xxx", "MessageDeduplicationId": "xxx", "MessageGroupId": "1" } }}Python代码如下:def lambda_handler(event, context): print(f'Received Event: {event}') try: body = json.loads(event.get('message').get('Body')) stage = body["stage"] directory= body["directory"] except Exception as e: return (e)我收到错误:{ "errorMessage": "Unable to marshal response: JSONDecodeError('Expecting property name enclosed in double quotes: line 1 column 2 (char 1)') is not JSON serializable", "errorType": "Runtime.MarshalError"}有人知道如何解决这个问题吗?
1 回答
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
您必须将 .txt 中的单引号替换为双引号body。因为在有效的 JSON 中,字符串以"
body = "{'stage': 'tst', 'directory': '\\\\\\\\mydomain\\\\data\\\\results', 'variableFilters': ['\\\\filters\\\\x1.txt', '\\\\filters\\\\a1.txt']}"
body = json.loads(body.replace('\'', '"'))
添加回答
举报
0/150
提交
取消