前提・実現したいこと
ここをサイトを参考にしてAWSLambda+DynamoDBでサーバレスでデータベースから値を取ってくるAPIチックなものを作ろうとしています。
サンプルと違うデータ構造で作成しようとしているのでエラーが出て
うまく対応できません。こういう場合にはDynamoDBのデータをどの様に持たせれば
うまく対応できますでしょうか。
item-category(Key) | item(String-set) |
---|---|
1 | {"110-1111", "112-0000", "113-0000", "114-0001"} |
2 | {"222-0001", "223-0002"} |
3 | {"300-0002", "300-0003", "301-0001", "309-0004", "310-0000", "333-0004"} |
発生している問題・エラーメッセージ
エラーは下記の様に型エラーでJSONをシリアライズできないという内容の様です。
Response: { "errorMessage": "{'112-0000', '110-1111', '114-0001', '113-0000'} is not JSON serializable", "errorType": "TypeError", "stackTrace": [ [ "/var/lang/lib/python3.6/json/__init__.py", 238, "dumps", "**kw).encode(obj)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 199, "encode", "chunks = self.iterencode(o, _one_shot=True)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 257, "iterencode", "return _iterencode(o, 0)" ], [ "/var/runtime/awslambda/bootstrap.py", 110, "decimal_serializer", "raise TypeError(repr(o) + \" is not JSON serializable\")" ] ] } Request ID: "f394c358-d9b9-11e8-bfcc-177cfa0a5a96" Function Logs: START RequestId: f394c358-d9b9-11e8-bfcc-177cfa0a5a96 Version: $LATEST An error occurred during JSON serialization of response: {'112-0000', '110-1111', '114-0001', '113-0000'} is not JSON serializable Traceback (most recent call last): File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps **kw).encode(obj) File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/var/runtime/awslambda/bootstrap.py", line 110, in decimal_serializer raise TypeError(repr(o) + " is not JSON serializable") TypeError: {'112-0000', '110-1111', '114-0001', '113-0000'} is not JSON serializable END RequestId: f394c358-d9b9-11e8-bfcc-177cfa0a5a96 REPORT RequestId: f394c358-d9b9-11e8-bfcc-177cfa0a5a96 Duration: 197.52 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 30 MB
該当のソースコード
python3.6
1import boto3 2 3dynamodb = boto3.resource('dynamodb') 4table = dynamodb.Table('item') 5 6def get_item(id): 7 response = table.get_item( 8 Key={ 9 'item-category': id 10 } 11 ) 12 return response['Item'] 13 14def lambda_handler(event, context): 15 item = get_item('1') 16 return item
試したこと
Stringにして通常の文字列を入れるとエラーにならずに値は取れました。
item-category(Key) | item(String) |
---|---|
5 | "4444" |
Response: { "value": "44444", "item-category": "5" } Request ID: "80bc4afd-d9bd-11e8-93ce-1bed5e34aeec"
string-Setにして下記の様な感じでデータを入れるとエラーとなりました。
item-category(Key) | item(String-set) |
---|---|
6 | {"itemname":"111-0000", "itemname":"122-0001"} |
Response: { "errorMessage": "{'\"itemname\":\"122-0001\"', '\"itemname\":\"111-0000\"'} is not JSON serializable", "errorType": "TypeError", "stackTrace": [ [ "/var/lang/lib/python3.6/json/__init__.py", 238, "dumps", "**kw).encode(obj)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 199, "encode", "chunks = self.iterencode(o, _one_shot=True)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 257, "iterencode", "return _iterencode(o, 0)" ], [ "/var/runtime/awslambda/bootstrap.py", 110, "decimal_serializer", "raise TypeError(repr(o) + \" is not JSON serializable\")" ] ] } Request ID: "e045d7f6-d9bd-11e8-9881-514ef66fa25e" Function Logs: START RequestId: e045d7f6-d9bd-11e8-9881-514ef66fa25e Version: $LATEST An error occurred during JSON serialization of response: {'"itemname":"122-0001"', '"itemname":"111-0000"'} is not JSON serializable Traceback (most recent call last): File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps **kw).encode(obj) File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/var/runtime/awslambda/bootstrap.py", line 110, in decimal_serializer raise TypeError(repr(o) + " is not JSON serializable") TypeError: {'"itemname":"122-0001"', '"itemname":"111-0000"'} is not JSON serializable END RequestId: e045d7f6-d9bd-11e8-9881-514ef66fa25e REPORT RequestId: e045d7f6-d9bd-11e8-9881-514ef66fa25e Duration: 186.88 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 30 MB
Stringにして以下の内容で。対応すると文字列として取得はできますが
エスケープが入ってしまいます。
item-category(Key) | item(String) |
---|---|
5 | "{"itemname":"222-0003"},{"itemname":"310-0456"}" |
{ "item": "{\"itemname\":\"222-0003\"},{\"itemname\":\"310-0456\"}" "item-category": "7" }
補足情報
どうして2カラム目のitemをjson形式にしようとしているかというと「item-category」によって登録する「item」の数量が可変するのでそれを管理したいと考えてのことです。
わざわざ複雑な管理方法にしなくてもこうすれば単純にできるよという回答があればそちらの方法でも構わないと考えております。ぜひ回答をお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。