AWS Developer Tools Blog

Storing JSON documents in Amazon DynamoDB tables

DynamoDBMapper is a high-level abstraction layer in the AWS SDK for Java that allows you to transform java objects into items in Amazon DynamoDB tables and vice versa. All you need to do is annotate your java class in a few places, and the mapper takes care of getting the objects in and out of the database.

DynamoDBMapper has a new feature that allows you to save an object as a JSON document in a DynamoDB attribute. To do this, simply annotate the class with @DynamoDBDocument, and the mapper does the heavy work of converting the object into a JSON document and storing it in DynamoDB. DynamoDBMapper also takes care of loading the java object from the JSON document when requested by the user.

Let’s say your application maintains the inventory of a car dealership in Amazon DynamoDB and uses DynamoDBMapper to save and retrieve data. One of the tables is Car, which holds information about a car and has name as its primary key. Here is how the java class looks for the table:

@DynamoDBTable(tableName = "Car")
public class Car {
		
    private String name;
    private int year;
    private String make;
    private List<String> colors;
    private Spec spec;

    @DynamoDBHashKey
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public int getYear() { return year; }
    public void setYear(int year) { this.year = year; }

    public String getMake() { return make; }
    public void setMake(String make) { this.make = make; }

    public List<String> getColors() { return colors; }
    public void setColors(List<String> colors) { this.colors = colors; }

    public Spec getSpec() { return spec; }
    public void setSpec(Spec spec) { this.spec = spec; }
}

@DynamoDBDocument
public class Spec {

    private String engine;
    private String wheelbase;
    private String length;
    private String width;
    private String height;

    public String getEngine() { return engine; }
    public void setEngine(String engine) { this.engine = engine; }

    public String getWheelbase() { return wheelbase; }
    public void setWheelbase(String wheelbase) { this.wheelbase = wheelbase; }

    public String getLength() { return length; }
    public void setLength(String length) { this.length = length; }

    public String getWidth() { return width; }
    public void setWidth(String width) { this.width = width; }

    public String getHeight() { return height; }
    public void setHeight(String height) { this.height = height; }

}

As you can see, the class Spec is modeled with a @DynamoDBDocument annotation. DynamoDBMapper converts an instance of Spec into a JSON document before storing it in DynamoDB. When stored in DynamoDB, an instance of the class Car will look like this:

{
   "name" : "IS 350",
   "year" : "2015",
   "make" : "Lexus",
   "colors" : ["black","white","grey"],
   "spec" : {
      "engine" : "V6",
      "wheelbase" : "110.2 in",
      "length" : "183.7 in",
      "width" : "71.3 in",
      "height" : "56.3 in"
   }
}

You can also apply other DyanmoDBMapper annotations like @DyanmoDBIgnore and @DynamoDBAttribute to the JSON document. For instance, model the height attribute of the Spec class with @DynamoDBIgnore.

@DynamoDBIgnore
public String getHeight() { return height; }
public void setHeight(String height) { this.height = height; }

The updated item in DynamoDB will look like this:

{
   "name" : "IS 350",
   "year" : "2015",
   "make" : "Lexus",
   "colors" : ["black","white","grey"],
   "spec" : {
      "engine" : "V6",
      "wheelbase" : "110.2 in",
      "length" : "183.7 in",
      "width" : "71.3 in"
   }
}

To learn more, check out our other blog posts and the developer guide

Do you want to see new features in DynamoDBMapper? Let us know what you think!