title: json: cannot unmarshal object into Go value of type string" aws dynamodb
body: <p>I am following this <a href="https://www.alexedwards.net/blog/serverless-api-with-go-and-aws-lambda" rel="nofollow noreferrer">post</a>, so far the simple version is working but the section using AWS dynamodb is not working for me.</p>
  <p>I have created an AWS dynamo table and inserted data</p>
  <pre class="lang-sh prettyprint-override"><code>aws dynamodb create-table --table-name Services --attribute-definitions AttributeName=Name,AttributeType=S --key-schema AttributeName=Name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
  </code></pre>
  <pre class="lang-sh prettyprint-override"><code>aws dynamodb put-item --table-name Services --item '{&quot;Name&quot;: {&quot;S&quot;: &quot;Lets Keep Safe&quot;}, &quot;ID&quot;: {&quot;N&quot;: &quot;3&quot;}, &quot;CreatedAt&quot;:  {&quot;S&quot;: &quot;2020-11-21&quot;}}'
  </code></pre>
  <p>main.go</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;github.com/aws/aws-lambda-go/lambda&quot;
  )
  
  // Service Names
  type Service struct {
      Name      string `json:&quot;name&quot;`
      ID        int    `json:&quot;id&quot;`
      CreatedAt string `json:&quot;created_at&quot;`
  }
  
  func show(ID string) (*Service, error) {
      // Fetch a specific book record from the DynamoDB database. We'll
      // make this more dynamic in the next section.
      // ser, err := getItem(ID)
      ser, err := getItem(&quot;2&quot;)
      if err != nil {
          return nil, err
      }
  
      return ser, nil
  }
  
  func main() {
      lambda.Start(show)
  }
  </code></pre>
  <p>db.go</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;github.com/aws/aws-sdk-go/aws&quot;
      &quot;github.com/aws/aws-sdk-go/aws/session&quot;
      &quot;github.com/aws/aws-sdk-go/service/dynamodb&quot;
      &quot;github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute&quot;
  )
  
  // Declare a new DynamoDB instance. Note that this is safe for concurrent
  // use.
  var db = dynamodb.New(session.New(), aws.NewConfig().WithRegion(&quot;eu-west-1&quot;))
  
  func getItem(ID string) (*Service, error) {
      // Prepare the input for the query.
      input := &amp;dynamodb.GetItemInput{
          TableName: aws.String(&quot;services&quot;),
          Key: map[string]*dynamodb.AttributeValue{
              &quot;ID&quot;: {
                  N: aws.String(ID),
              },
          },
      }
  
      // Retrieve the item from DynamoDB. If no matching item is found
      // return nil.
      result, err := db.GetItem(input)
      if err != nil {
          return nil, err
      }
      if result.Item == nil {
          return nil, nil
      }
  
      // The result.Item object returned has the underlying type
      // map[string]*AttributeValue. We can use the UnmarshalMap helper
      // to parse this straight into the fields of a struct. Note:
      // UnmarshalListOfMaps also exists if you are working with multiple
      // items.
      ser := new(Service)
      err = dynamodbattribute.UnmarshalMap(result.Item, ser)
      if err != nil {
          return nil, err
      }
  
      return ser, nil
  }
  </code></pre>
  <p>Invoking the lambda results in</p>
  <pre class="lang-json prettyprint-override"><code>{&quot;errorMessage&quot;:&quot;json: cannot unmarshal object into Go value of type string&quot;,&quot;errorType&quot;:&quot;UnmarshalTypeError&quot;}
  </code></pre>