Matt
Matt

Reputation: 88047

C# serialize DynamoDB item to JSON

In my C# lambda I retrieve an item from DynamoDB. It is returned as a Dictionary<string, AttributeValue>. Is there a good way to serialize that to JSON?

The AttributeValue class exposes a bunch of properties to retrieve values of different types. If you do a simple serialization each one of those properties shows up in the JSON, most of them null, and makes a really messy object.

I want it to recognize a map and turn it into a JSON object, recognize a list and turn it into a JSON list. Basically, I want the object the Item represents.

Upvotes: 2

Views: 5061

Answers (1)

Regan
Regan

Reputation: 104

You can use an alternate approach in C# to connect to DynamoDB using the DocumentModel. This DocumentModel has a ToJson() function.

Table table = Table.LoadTable(client, "yourtable");
Document document = table.GetItem(123);
string strJSON = document.ToJson();

Upvotes: 5

Related Questions