title: Serverless Framework and DynamoDB: Unexpected token in JSON at JSON.parse
body: <p>Hi I followed this <a href="https://serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb/" rel="nofollow noreferrer">Serverless + AWS REST API tutorial</a> and it went great, I got it to work.</p>
  
  <p>Now, I'm trying to modify it but have hit a wall while trying to submit data into the DynamoDB table.</p>
  
  <p>Using Postman to submit a valid JSON object I get a 502 response. If I test the function in Lambda, I get the following error:</p>
  
  <pre><code>{
    "errorType": "SyntaxError",
    "errorMessage": "Unexpected token o in JSON at position 1",
    "trace": [
      "SyntaxError: Unexpected token o in JSON at position 1",
      "    at JSON.parse (&lt;anonymous&gt;)",
      "    at Runtime.module.exports.submit [as handler] (/var/task/api/interview.js:11:28)",
      "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)",
      "    at process._tickCallback (internal/process/next_tick.js:68:7)"
    ]
  }
  </code></pre>
  
  <p>After searching for solutions, what I found out is that it seem like the <code>event</code> that is being passed as <code>JSON.parse(event)</code>is undefined.</p>
  
  <p>Here's the serverless.yml:</p>
  
  <pre><code>service: interview
  
  frameworkVersion: "&gt;=1.1.0 &lt;2.0.0"
  
  provider:
    name: aws
    runtime: nodejs10.x
    stage: dev
    region: us-east-1
    environment:
      INTERVIEW_TABLE: ${self:service}-${opt:stage, self:provider.stage}
      INTERVIEW_EMAIL_TABLE: "interview-email-${opt:stage, self:provider.stage}"
    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:Query
          - dynamodb:Scan
          - dynamodb:GetItem
          - dynamodb:PutItem
        Resource: "*"
  
  resources:
    Resources:
      CandidatesDynamoDbTable:
        Type: 'AWS::DynamoDB::Table'
        DeletionPolicy: Retain
        Properties:
          AttributeDefinitions:
            -
              AttributeName: "id"
              AttributeType: "S"   
          KeySchema:
            -
              AttributeName: "id"
              KeyType: "HASH"
          ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
          StreamSpecification:
            StreamViewType: "NEW_AND_OLD_IMAGES"
          TableName: ${self:provider.environment.INTERVIEW_TABLE}
  
  functions:
    interviewSubmission:
      handler: api/interview.submit
      memorySize: 128
      description: Submit interview information and starts interview process.
      events:
        - http: 
            path: interviews
            method: post
  </code></pre>
  
  <p>and the interview.js</p>
  
  <pre><code>'use strict';
  
  const uuid = require('uuid');
  const AWS = require('aws-sdk'); 
  
  AWS.config.setPromisesDependency(require('bluebird'));
  
  const dynamoDb = new AWS.DynamoDB.DocumentClient();
  
  module.exports.submit = (event, context, callback) =&gt; {
    const requestBody = JSON.parse(event);
    const fullname = requestBody.fullname;
    const email = requestBody.email;
    const test = requestBody.test;
    const experience = requestBody.experience;
  
    if (typeof fullname !== 'string' || typeof email !== 'string' || typeof experience !== 'number') {
      console.error('Validation Failed');
      callback(new Error('Couldn\'t submit interview because of validation errors.'));
      return;
    }
  
    submitInterviewP(interviewInfo(fullname, email, experience, test))
      .then(res =&gt; {
        callback(null, {
          statusCode: 200,
          body: JSON.stringify({
            message: `Sucessfully submitted interview with email ${email}`,
            interviewId: res.id
          })
        });
      })
      .catch(err =&gt; {
        console.log(err);
        callback(null, {
          statusCode: 500,
          body: JSON.stringify({
            message: `Unable to submit interview with email ${email}`
          })
        })
      });
  };
  
  
  const submitInterviewP = interview =&gt; {
    console.log('Submitting interview');
    const interviewInfo = {
      TableName: process.env.INTERVIEW_TABLE,
      Item: interview,
    };
    return dynamoDb.put(interviewInfo).promise()
      .then(res =&gt; interview);
  };
  
  const interviewInfo = (fullname, email, experience,test) =&gt; {
    const timestamp = new Date().getTime();
    return {
      id: uuid.v1(),
      fullname: fullname,
      email: email,
      experience: experience,
      test: test,
      submittedAt: timestamp,
      updatedAt: timestamp,
    };
  };
  </code></pre>
  
  <p>If I replace the <code>event</code> param for a valid JSON object and then deploy again. I'm able to successfully insert the object into dynamoDB.</p>
  
  <p>Any clues? Please let me know if there's anything I missing that could help.</p>
  
  <p>Thanks!</p>