Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerializationException in posting new Records via Dynamodb Proxy Service in API

I am getting

"__type": "com.amazon.coral.service#SerializationException"

as a reply in postman & in test console in API Gateway

Trying to post a record directly to dynamodb using API Proxy Services.. I am referring this AWS Article - https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/

Here's my Mapping

{ 
    "TableName": "TableNameGoesHere",
    "Item": {
    "id" : "$context.requestId"
    "eventName" : "$input.path('$.eventName')",
    "timestamp" : $input.path('$.timestamp'),
    "answers": "$util.parseJson($input.path('$.answers'))"
    }
}

Update: I did as asked ... and it worked but now if I try to add a Array of JSON Objects it gives me the above same error - here's what I am trying to do now. Please help - Couldnt find anything on google as well

#set($inputRoot = $input.path('$'))
{ 
    "TableName": "Answer",
    "Item": {
    "id": {
            "S": "$context.requestId"
            },
    "eventName": {
            "S": "$input.path('$.eventName')"
            },
    "timestamp" : {
            "N": "$input.path('$.timestamp')"
            },
    "answers": {
            "S": "$input.path('$.answers')"
            },
    "Line": {
    "S" : "[
#foreach($elem in $inputRoot.Line)
    {
      "questionID" : "$elem.questionID",
      "answer" : "$elem.answer"
    }#if($foreach.hasNext),#end

#end
  ]" }
    }
}
like image 414
Abdeali Chandanwala Avatar asked Nov 10 '16 14:11

Abdeali Chandanwala


2 Answers

To address the challenge of having Array of Objects as part of the payload.

For Request Payload

{
    "emailId": "[email protected]",
    "responses": [
        {
            "question": "q1",
            "answer": "a1"
        },
        {
            "question": "q2",
            "answer": "a2"
        }
    ]
}

Template would be

#set($inputRoot = $input.path('$'))
{
    "TableName": "Customers",
    "Item": {
        "leadId": {
            "S": "$context.requestId"
        },
        "emailId": {
            "S": "$input.path('$.emailId')"
        },
        "responses": {
            "L": [            // List type
            #foreach($elem in $inputRoot.responses) // Loop thru array
                {
                    "M": {        // Map type
                        "answer": {
                            "S": "$elem.answer"
                        },
                        "question": {
                            "S": "$elem.question"
                        }
                    }
                }
                #if($foreach.hasNext),#end
            #end
            ]
        }
    }
}

Integration Response Template (similar structure as request)

#set($inputRoot = $input.path('$'))
{
  "$results": [
    #foreach($elem in $inputRoot.Items)
    {
      "emailId": "$elem.emailId.S",
      "responses": [
          #foreach($resp in $elem.responses.L)
          {
           "question": "$resp.M.question.S",
           "answer": "$resp.M.answer.S"
          }
          #if($foreach.hasNext),#end
          #end
      ]
    }
    #if($foreach.hasNext),#end
    #end
  ]
}
like image 138
Varun Verma Avatar answered Oct 03 '22 01:10

Varun Verma


Your mapping template doesn't match the DynamoDB format. It should be something like,

{ 
    "TableName": "Comments",
    "Item": {
        "commentId": {
            "S": "$context.requestId"
        },
        "pageId": {
            "S": "$input.path('$.pageId')"
            },
        "userName": {
            "S": "$input.path('$.userName')"
        },
        "message": {
            "S": "$input.path('$.message')"
        }
    }
}
like image 20
Ka Hou Ieong Avatar answered Oct 03 '22 00:10

Ka Hou Ieong