I have successfully queried a DynamoDB dataset and returned the last item associated with the primary key. The item data consists of the following:
`{
"machineID": {
"N": "1322"
},
"ts": {
"N": "1646299793339"
},
"data": {
"M": {
"average-speed": {
"N": "0"
},
"in-machine-mode": {
"N": "0"
},
"run-speed": {
"N": "40"
},
"total-packs-life": {
"N": "0"
},
"recipe-loaded-id": {
"N": "1"
},
"total-packs-shift": {
"N": "0"
},
"message-banner": {
"N": "0"
},
"ts": {
"N": "1646299793339"
}
}
}
}`
I want to extract the Map values in JSON format but cannot find any instruction/tutorials on how this is achieved.
Here is the basic code to query and extract the last record relating to a machine number.
`var response = await client.QueryAsync(request);
var result = response.Items;
foreach(Dictionary<string, AttributeValue> item in result)
{
foreach(KeyValuePair<string, AttributeValue> kvp in item )
{
var attribute = kvp.Key;
var value = kvp.Value;
Console.WriteLine(attribute + " " + (value.N == null ? "" : value.N));
}
}`
Output is as follows: ts 1646299793339 machineID 1322 data
How do I get the M attribute? I would appreciate help to resolve my understanding.
See above for trials
Having spent some time searching for an answer, and in the process learning more about how dyanamoDB works, I now have a solution which works.
Essential I needed to convert the attribute key-value pair from the query into a document.
Achieved using:
var response = await dynamoDBClient.QueryAsync(request);
var attributes = response.Items.FirstOrDefault();
var doc = Document.FromAttributeMap(attributes);
From this its a simple case of converting doc to a JSON Object
Console.WriteLine(doc.ToJson());
Maybe there is another way? Comments appreciated.