【问题标题】:AWS Lambda JSON to DynamoDB KeyError Serializing JSONAWS Lambda JSON to DynamoDB KeyError 序列化 JSON
【发布时间】:2017-10-30 18:30:40
【问题描述】:

我正在 AWS 上设置我的 lambda 函数,并希望将我的 JSON 插入 DynamoDB (NoSQL)。我认为这与 JSON (decimal_serializer) 的序列化有关,但我可能错了。感谢您的帮助!

序列化此数据时出现以下错误:

错误信息:

{
  "errorMessage": "'male_confidence'",
  "errorType": "KeyError"
}

JSON 数据插入:

{
  "device_id": "abc876",
  "recorded_at": "1496136878",
  "customers": [
    {
      "male_confidence": "0.2",
      "female_confidence": "0.8"
    },
    {
      "male_confidence:": "0.1",
      "female_confidence": "0.9"
    }
  ]
}

Lambda 函数处理程序

import boto3
import json

def lambda_handler(event, context):
    # TODO implement
    client = boto3.client('dynamodb')
    for customer in event['customers']:
        client.put_item(TableName="cv_data_1", Item={'device_id': {"S": event['device_id']}, 'male_confindence': {"N": customer['male_confidence']}, 'female_confidence': {"N": customer['female_confidence']}, "timestamp":{ "N": event['recorded_at']}})
    print('Successfully processed %s items.' % str(len(event['customers'])))

AWS 输出日志错误(详细):

During handling of the above exception, another exception occurred:

09:23:38
Traceback (most recent call last):

09:23:38
File "/var/runtime/awslambda/bootstrap.py", line 463, in <module>

09:23:38
main()

09:23:38
File "/var/runtime/awslambda/bootstrap.py", line 459, in main

09:23:38
handle_event_request(request_handler, invokeid, event_body, context_objs, invoked_function_arn)

09:23:38
File "/var/runtime/awslambda/bootstrap.py", line 240, in handle_event_request

09:23:38
result = to_json(result)

09:23:38
File "/var/runtime/awslambda/bootstrap.py", line 215, in to_json

09:23:38
return json.dumps(obj, default=decimal_serializer)

09:23:38
File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps

09:23:38
**kw).encode(obj)

09:23:38
File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode

09:23:38
chunks = self.iterencode(o, _one_shot=True)

09:23:38
File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode

09:23:38
return _iterencode(o, 0)

09:23:38
File "/var/runtime/awslambda/bootstrap.py", line 104, in decimal_serializer

09:23:38
raise TypeError(repr(o) + " is not JSON serializable")

09:23:38
TypeError: <FrameSummary file /var/task/lambda_function.py, line 8 in lambda_handler> is not JSON serializable

【问题讨论】:

  • 从错误消息中,male_confidence 似乎在customer 之一中丢失。 (没有双关语的意思)。您可以在 lambda 处理程序中打印event 以便查看event 包含的数据吗?输出将在 cloudwatch 日志中。
  • {'device_id': 'abc876', 'recorded_at': '1496136878', 'customers': [{'male_confidence': '0.2', 'female_confidence': '0.8'}, {'male_confidence:': '0.1', 'female_confidence': '0.9'}]},但如果我在循环中输出print(customer),它会返回dict,见下文。
  • 我在循环中打印了customer['male_confidence'],它返回给我0.2,第二个循环因上述错误而中断。
  • {'device_id': 'abc876', 'recorded_at': '1496136878', 'customers': [{'male_confidence': '0.2', 'female_confidence': '0.8'}, {'male_confidence:': '0.1', 'female_confidence': '0.9'}]} HELLO {'male_confidence': '0.2', 'female_confidence': '0.8'} ##### 0.2 0.8 HELLO {'male_confidence:': '0.1', 'female_confidence': '0.9'} 'male_confidence': KeyError Traceback (most recent call last): File "/var/task/lambda_function.py", line 14, in lambda_handler mc = customer['male_confidence'] KeyError: 'male_confidence'

标签: python json amazon-web-services aws-lambda boto3


【解决方案1】:

错字:

  "male_confidence:": "0.1",
  "female_confidence": "0.9"

"male_confidence:"

【讨论】:

  • 非常感谢!我快疯了:。
【解决方案2】:

male_confidence: 后面有错字,请注意多余的:。试试这个

{
  "device_id": "abc876",
  "recorded_at": "1496136878",
  "customers": [
    {
      "male_confidence": "0.2",
      "female_confidence": "0.8"
    },
    {
      "male_confidence": "0.1",
      "female_confidence": "0.9"
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2021-01-20
    • 2017-05-19
    • 2017-11-29
    • 2021-04-12
    • 2013-02-17
    相关资源
    最近更新 更多