Reputation: 2396
My code is like this, I just use a scan of DynamoDB, and change the result to JSON
var client = new AmazonDynamoDBClient();
var request = new ScanRequest
{
TableName = "wow_chat_msg",
};
var response = client.Scan(request);
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(response.Items);
But after that the returned JSON is like this:
[{\"Name\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"ABC\",\"SS\":[]},\"Type\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"CDE\",\"SS\":[]},\"msg_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"1010\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]},\"group_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"1\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]}},{\"Name\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"ABC\",\"SS\":[]},\"Type\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"CDE\",\"SS\":[]},\"msg_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"3\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]}},{\"Name\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"Chong\",\"SS\":[]},\"Type\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"Peter\",\"SS\":[]},\"msg_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"2\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]}},{\"Name\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"ABC\",\"SS\":[]},\"Type\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"CDE\",\"SS\":[]},\"msg_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"1011\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]}},{\"Name\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"Chan\",\"SS\":[]},\"Type\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":null,\"NS\":[],\"NULL\":false,\"S\":\"Rico\",\"SS\":[]},\"msg_uid\":{\"B\":null,\"BOOL\":false,\"IsBOOLSet\":false,\"BS\":[],\"L\":[],\"IsLSet\":false,\"M\":{},\"IsMSet\":false,\"N\":\"1\",\"NS\":[],\"NULL\":false,\"S\":null,\"SS\":[]}}]
The escape character is because I change it to string. But I want to ask how to get rid of the unnecessary attributes? Like I just want the "2" in the json value, but there are too many other attributes like "S", "SS", "BOOL" .... etc I just want a pretty JSON.
Please help, I have search a lot of documents and want to seek solution.
Upvotes: 0
Views: 3329
Reputation: 221
I have a solution that returns a nice JSON. This is for a Lambda function:
public List<dynamic> GetDynamoData(string dynamoTableName, ILambdaContext context) {
Amazon.DynamoDBv2.AmazonDynamoDBConfig amazonDynamoDBConfig = new Amazon.DynamoDBv2.AmazonDynamoDBConfig { RegionEndpoint = RegionEndpoint.XWZ};
Amazon.DynamoDBv2.AmazonDynamoDBClient client = new Amazon.DynamoDBv2.AmazonDynamoDBClient(amazonDynamoDBConfig);
Table myTable = Table.LoadTable(client,dynamoTableName);
ScanFilter scanFilter = new ScanFilter();
scanFilter.AddCondition("IDCOLULM", ScanOperator.GreaterThan, 123465); //Whatever your filter is
Search search = myTable.Scan(scanFilter);
List<dynamic> results = new List<dynamic>();
do
{
var docList = search.GetNextSetAsync();
docList.Result.ForEach(doc=> {
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(doc.ToJson());
results.Add(x);
});
} while (!search.IsDone);
return results;
}
Upvotes: 1
Reputation: 2396
Just found the answer.
Using DynamoDB's low level api will return a lot of unnecessary data for low level usage. So we can use the high level document model.
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanMidLevelDotNet.html
Use Table.scan, then use your own method to combine the Object of document.ToJSON(), here I use ArrayList to combine all of them.
Then after I got a list of Object, then return to the client side. Everything is beautiful.
Upvotes: 0