Reputation: 3
I am using the AWS service proxy to expose DynamoDB as API. For PutItem it works fine, but I don't have idea how to update a array of object. Below "paymentDetail" is the list of Map, I can post to DynamoDB, but I failed for update the JSON payload. Not sure DynamoDB support this or not, I always got this message:
{"__type": "com.amazon.coral.service#SerializationException"}
Can anybody share your ideas how to use mapping template to update JSON payload? Thanks in advance.
Here is my code, please advise and help.Please kindly let me know in case that the information is not sufficient.
--------- Request Method for UpdateItem ------------
{
"TableName": "claim",
"Key": {
"claimNumber": {
"S": "$input.params("claimNumber")"
}
},
"UpdateExpression": "set claimStatus = :claimStatus, statusUpdateDate = :statusUpdateDate, netClaimAmount = :netClaimAmount, paymentDetail = :paymentDetail, updatedAt = :updatedAt",
"ConditionExpression": "claimNumber = :claimNumber",
"ExpressionAttributeValues": {
":claimNumber": {"S": "$input.params("claimNumber")"},
":claimStatus": {"S": "$input.path("$.claimStatus")"},
":statusUpdateDate": {"S": "$input.path("$.statusUpdateDate")"},
":netClaimAmount": {"N": "$input.path("$.netClaimAmount")"},
":paymentDetail": {"L": "$input.path("$.paymentDetail")"},
":updatedAt": {"S": "$input.path("$.updatedAt")"}
},
"ReturnValues": "UPDATED_NEW"
}
----------test data for UpdateItem---------
{
"claimNumber": "000-00-000959",
"updatedAt":"2017-09-10",
"claimStatus": "close",
"statusUpdateDate": "2017-09-13",
"netClaimAmount": 60000000,
"paymentDetail": [
{
"coverage": "AAA",
"claimPaymentDate": "2017-09-03",
"claimAmount": "10000000",
"costCategory": "1",
"costType": "A"
},
{
"coverage": "BBB",
"claimPaymentDate": "2017-09-03",
"claimAmount": "20000000",
"costCategory": "2",
"costType": "B"
},
{
"coverage": "CCC",
"claimPaymentDate": "2017-09-03",
"claimAmount": "30000000",
"costCategory": "3",
"costType": "C"
}
]
}
---PutItem for your information---
#set($inputRoot = $input.path('$'))
{
"TableName": "claim",
"Item": {
"claimNumber": {
"S": "$input.path("$.claimNumber")"
},
"policyNumber": {
"S": "$input.path("$.policyNumber")"
},
"productName": {
"S": "$input.path("$.productName")"
},
"lossDate": {
"S": "$input.path("$.lossDate")"
},
"lossTime": {
"S": "$input.path("$.lossTime")"
},
"reportedDate": {
"S": "$input.path("$.reportedDate")"
},
"lossCause": {
"S": "$input.path("$.lossCause")"
},
"description": {
"S": "$input.path("$.description")"
},
"prefectureCode": {
"S": "$input.path("$.prefectureCode")"
},
"city": {
"S": "$input.path("$.city")"
},
"address": {
"S": "$input.path("$.address")"
},
"reportedByType": {
"S": "$input.path("$.reportedByType")"
},
"createdAt": {
"S": "$input.path("$.createdAt")"
},
"updatedAt": {
"S": "$input.path("$.updatedAt")"
},
"claimImageId": {
"S": "$input.path("$.claimImageId")"
},
"contact": {
"M": {
"lastName": {
"S": "$input.path("$.contact.lastName")"
},
"firstName": {
"S": "$input.path("$.contact.firstName")"
},
"postCode": {
"S": "$input.path("$.contact.postCode")"
},
"prefectureCode": {
"S": "$input.path("$.contact.prefectureCode")"
},
"city": {
"S": "$input.path("$.contact.city")"
},
"address": {
"S": "$input.path("$.contact.address")"
},
"homePhone": {
"S": "$input.path("$.contact.homePhone")"
},
"email": {
"S": "$input.path("$.contact.email")"
}
}
},
"claimStatus": {
"S": "$input.path("$.claimStatus")"
},
"statusUpdateDate": {
"S": "$input.path("$.statusUpdateDate")"
},
"netClaimAmount": {
"N": "$input.path("$.netClaimAmount")"
},
"paymentDetail": {
"L":[
#foreach($elem in $inputRoot.paymentDetail){
"M": {
"coverage": {
"S": "$elem.coverage"
},
"claimPaymentDate": {
"S": "$elem.claimPaymentDate"
},
"claimAmount": {
"S": "$elem.claimAmount"
},
"costCategory": {
"S": "$elem.costCategory"
},
"costType": {
"S": "$elem.costType"
}
}
}#if($foreach.hasNext),#end
#end
]
}
}
}
Upvotes: 0
Views: 2751
Reputation: 141
Your body mapping template seems to have issues. Please see below the corrected one.
#set($inputRoot = $input.path('$'))
{
"TableName": "claim",
"Key": {
"claimNumber": {
"S": "$input.path("$.claimNumber")"
}
},
"UpdateExpression": "set claimStatus = :claimStatus, statusUpdateDate = :statusUpdateDate, netClaimAmount = :netClaimAmount, paymentDetail = :paymentDetail, updatedAt = :updatedAt",
"ConditionExpression": "claimNumber = :claimNumber",
"ExpressionAttributeValues": {
":claimNumber": {"S": "$input.path("$.claimNumber")"},
":claimStatus": {"S": "$input.path("$.claimStatus")"},
":statusUpdateDate": {"S": "$input.path("$.statusUpdateDate")"},
":netClaimAmount": {"N": "$input.path("$.netClaimAmount")"},
":updatedAt": {"S": "$input.path("$.updatedAt")"},
":paymentDetail": {
"L":[
#foreach($elem in $inputRoot.paymentDetail){
"M": {
"coverage": {
"S": "$elem.coverage"
},
"claimPaymentDate": {
"S": "$elem.claimPaymentDate"
},
"claimAmount": {
"S": "$elem.claimAmount"
},
"costCategory": {
"S": "$elem.costCategory"
},
"costType": {
"S": "$elem.costType"
}
}
}#if($foreach.hasNext),#end
#end
]
}
},
"ReturnValues": "UPDATED_NEW"
}
Upvotes: 4