2017-05-30 284 views
0

我在AWS上設置了我的lambda函數,並希望將我的JSON插入到DynamoDB(NoSQL)中。我認爲它與JSON(decimal_serializer)的序列化有關,但我可能是錯的。感謝幫助!AWS Lambda JSON到DynamoDB KeyError序列化JSON

錯誤味精:

在序列化這個數據我獲得以下錯誤

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

JSON數據到INSERT:

{ 
    "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 
+0

從錯誤信息中可以看出''客戶'之一的'male_confidence'缺失。 (沒有雙關語意)。你可以在lambda處理程序中打印'event',以便查看'event'包含的數據嗎?輸出將在cloudwatch日誌中。 – nightgaunt

+0

'{'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''。 – zer02

+0

我在循環中打印了'customer ['male_confidence']',它返回了'0.2',第二個循環打破了上面的錯誤。 – zer02

回答

1

錯字:

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

「male_confidence:」

+0

非常感謝!我瘋了':'。 – zer02

1

你有一個錯字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" 
    } 
    ] 
}