AWS Database Blog
Working with JSON data in Amazon DynamoDB
Amazon DynamoDB allows you to store JSON objects into attributes and perform many operations on these objects, including filtering, updating, and deleting. This is a very powerful capability because it allows applications to store objects (JSON data, arrays) directly into DynamoDB tables, and still retain the ability to use nested attributes within these objects in their database operations. In this post, we explore some of the things that you can do with JSON objects in DynamoDB.
Installing DynamoDB shell (ddbsh)
You can install DynamoDB shell (ddbsh
) for Mac using homebrew, and for Ubuntu using pre-built binaries that are available on github. Refer to the “Using prebuilt binaries” section.
Solution overview
We use DynamoDB shell (ddbsh
) to illustrate these capabilities. To learn more, refer to Query data with DynamoDB Shell – a command line interface for Amazon DynamoDB. One of the objectives of ddbsh
is to provide a simple and intuitive environment for newcomers to DynamoDB to get started by running familiar SQL-like commands.
The query language in ddbsh
is derived from SQL and has DynamoDB-specific extensions.
For this post, we use a simple DynamoDB table that tracks information about fast food orders. The following code is an example order:
An order that was completed looks like the following:
An order that had three items looks like the following:
In the following sections, we show how to create a table and insert some sample data, query the data, update items, and add and delete attributes. We also discuss how to turn SQL into a set of API calls from your application.
Prerequisites
To get started, you need to have DynamoDB shell installed. For more information, refer to Query data with DynamoDB Shell – a command line interface for Amazon DynamoDB.
Prepare your data
Create a DynamoDB table with the following code:
Next, populate the four rows into DynamoDB with the following code:
Now you’re ready to start querying your data.
Query your data
You can find the first item in order 14 by running the following query:
You can also perform other queries, such as finding all the order lines that include a side of water that aren’t yet complete:
Here, DynamoDB unpacked the JSON object (details
), found the attribute called sides
, and looked in that array to see whether there was one that included water.
In the following query, an order of fries is ready, and we want to know which order should it be sent to:
Similarly, if a burger is ready, we can determine which order should it be attached to:
Update items
You can also update these orders with DynamoDB shell. For example, the customer who placed order 14, item 1, changed their mind and instead of water would like a soda:
Observe that the update references items in the array details.sides
by their index. The second item (index 1) is updated here.
Add attributes to and delete attributes from an embedded object
If a customer has a special request, you can add it to the order:
If the customer who ordered the BLT sandwich decided they didn’t want the water after all, we can use the following code:
Translate SQL to the DynamoDB API
When writing applications with DynamoDB, it’s useful to quickly prototype the action you want to perform using SQL, but you then might want to turn it into a set of API calls from your application. ddhsh
helps you understand how to code the UPDATE statement shown earlier using the EXPLAIN command:
EXPLAIN shows that the query (which had a full primary key, ono
and ino
) can be expressed as an UpdateItem()
API call. The table name and the key are specified. An update expression is encoded as follows:
This is done to handle any reserved words that may exist in your schema. By replacing attribute names with placeholders, you can safely perform this update. Here ddbsh
changed details
to #aTaa1
and sides to #aTaa2
. The value (soda
) was replaced with the placeholder :vTaa1
. Because DynamoDB UpdateItem()
will insert an item if one doesn’t already exist, ddbsh
emits a check, where #aTaa3
is a placeholder for the attribute ono
:
Because the Key condition provided the full primary key, if an item matching this primary key existed, then the attribute ono
would exist in the item.
The following code is another example of translating a request into an API call:
Clean up
When you’re done with your experiments, remember to remove any tables you might have created to avoid incurring costs. You can use DROP TABLE in ddbsh
to do so:
Conclusion
DynamoDB allows you to store JSON data into an attribute. Then you can query data and perform restrictions and modifications to attributes inside the JSON object.
We’ve heard from customers and prospects that they want resources that will help them learn the DynamoDB API. We’re experimenting with some solutions to this, such as the EXPLAIN command in ddbsh
, which we demonstrated in this post. We would appreciate your feedback on this tool. What can we add to ddbsh
that would make it easier for you to use DynamoDB?
Finally, if there are specific functions or functionalities that you would like to see implemented in ddbsh
, let us know by entering a support ticket or by emailing me directly at amrithie (at) amazon.com.
About the author
Amrith Kumar is a Senior Principal Engineer in Amazon Web Services and works on Amazon DynamoDB.