DynamoDB
JSON
Unmarshall
AWS
Database

Unmarshall DynamoDB JSON

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

DynamoDB does not return plain JSON values in its low-level wire format. Instead, each attribute is wrapped in an AttributeValue shape such as S, N, BOOL, M, or L. Unmarshalling is the step that converts that typed DynamoDB representation into a normal language-native object.

Understand the DynamoDB Attribute Format

A low-level DynamoDB item looks like this:

json
1{
2  "id": { "S": "123" },
3  "count": { "N": "42" },
4  "active": { "BOOL": true },
5  "tags": { "L": [{ "S": "a" }, { "S": "b" }] }
6}

This is valid DynamoDB JSON, but it is awkward to use directly in application logic. Most code would rather work with something like:

json
1{
2  "id": "123",
3  "count": 42,
4  "active": true,
5  "tags": ["a", "b"]
6}

That conversion is what unmarshalling does.

Use unmarshall in the AWS SDK for JavaScript

In the AWS SDK for JavaScript v3, the standard helper lives in @aws-sdk/util-dynamodb.

javascript
1import { unmarshall } from "@aws-sdk/util-dynamodb";
2
3const item = {
4  id: { S: "123" },
5  count: { N: "42" },
6  active: { BOOL: true },
7  tags: { L: [{ S: "a" }, { S: "b" }] }
8};
9
10const result = unmarshall(item);
11console.log(result);

This returns a plain JavaScript object that is much easier to use in business logic, templating, or API responses.

The important point is that unmarshall expects the low-level DynamoDB AttributeValue map, not an already-unwrapped object. If you pass it the wrong shape, the output will be wrong or the call will fail.

Know When You Do Not Need to Unmarshall Manually

If you use higher-level DynamoDB helpers such as the document client, the SDK can often do this conversion for you automatically. In JavaScript, DynamoDBDocumentClient is designed for that convenience layer.

So there are two common patterns:

  • low-level client plus explicit marshall and unmarshall
  • document client plus automatic conversion

Manual unmarshalling is still useful when you are working with raw stream records, debugging low-level responses, or dealing with code that intentionally stays close to DynamoDB's native wire format.

Be Careful with Numbers and Nested Data

DynamoDB stores numbers as strings in the low-level format, and the SDK converts them during unmarshalling. That is convenient, but you still need to think about numeric precision in your application if the values are large or exact decimal handling matters.

Nested maps and lists are also common in real items, and unmarshall handles them recursively. That is one reason it is usually better than handwritten conversion logic.

javascript
1import { unmarshall } from "@aws-sdk/util-dynamodb";
2
3const item = {
4  profile: {
5    M: {
6      name: { S: "Ada" },
7      scores: { L: [{ N: "10" }, { N: "20" }] }
8    }
9  }
10};
11
12console.log(unmarshall(item));

The nested M and L structures are converted into ordinary nested JavaScript objects and arrays.

Common Pitfalls

The most common mistake is trying to unmarshall data that is already plain JSON. The helper is for DynamoDB AttributeValue structures, not for arbitrary objects.

Another issue is mixing the low-level client and the document client without realizing which layer has already converted the data. That can lead to double-processing or confusing shape mismatches.

People also sometimes forget that DynamoDB numbers arrive as string-encoded values in the low-level format. Unmarshalling helps, but precision-sensitive code should still verify how those values are represented downstream.

Summary

  • DynamoDB low-level responses use typed AttributeValue wrappers such as S, N, BOOL, M, and L.
  • Unmarshalling converts that representation into normal language-native objects.
  • In the AWS SDK for JavaScript v3, use unmarshall from @aws-sdk/util-dynamodb.
  • Document-client layers often handle this conversion automatically, so manual unmarshalling is not always necessary.
  • Pay attention to number handling and nested structures when working with raw DynamoDB items.

Course illustration
Course illustration

All Rights Reserved.