Write data into DynamoDB from json file

Manoj Kengudelu
2 min readNov 7, 2023
Photo by Jan Antonin Kolar on Unsplash

One of the challenges is creating initial data set in dynamoDB. This data could be used for performance testing. AWS SDK go could be used to generate such data by reading json file.

Pre-requisite :

  • golang
  • table created in aws dynamodb
  • aws credentials in local

Solution :

package main

import (
"encoding/json"
"io/ioutil"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"

"fmt"
"log"
)

type Record struct {
Id string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}


// Get table items from JSON file
func getItems() []Record {
raw, err := ioutil.ReadFile("./.user_data.json")
if err != nil {
log.Fatalf("Got error reading file: %s", err)
}

var items []Record
json.Unmarshal(raw, &items)
return items
}

func main() {
// snippet-start:[dynamodb.go.create_item.session]
// Initialize a session that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials
// and region from the shared configuration file ~/.aws/config.


sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))

// Create DynamoDB client
svc := dynamodb.New(sess)

// Get table items from .user_data.json
items := getItems()


// Create item in table Users
tableName := "users"

for _, item := range items {
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
log.Fatalf("Got error marshalling map: %s", err)
}

input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}

_, err = svc.PutItem(input)
if err != nil {
log.Fatalf("Got error calling PutItem: %s", err)
}


fmt.Println("Successfully added '" + item.FirstName + "' to table " + tableName)
}
}

user_data.json would look like following

[
{
"Id": "200121a0-48d3-4349-942a-4ed239d34112",
"FirstName": "John",
"LastName": "Denver"
},

{
"Id": "b59c6349-9e59-4a6f-89e5-32121e0abf0f",
"FirstName": "Chris",
"LastName": "Martin"
}
]

--

--