The scenario where you convert one input Json format to another output Json is not uncommon. Before C# dynamic and ExpandoObject you would serialize the input Json to POCO model classes and use a Factory class to convert to another set of POCO model classes, that then would be serialized to Json.
With the dynamic type and the ExpandoObject you have another weapon of choice, as you can deserialize the input Json to a dynamic object, and convert the contents to another dynamic object that is serialized. Imagine the following input and output Json formats:
Input format:
{ "username": "someuser@somewhere.com", "timeStamp": "2017-09-20 13:50:16.560", "attributes": { "attribute": [{ "name": "Brian", "count": 400 }, { "name": "Pedersen", "count": 100 }] } }
Output format:
{ "table": "USER_COUNT", "users": [{ "uid": "someuser@somewhere.com", "rows": [{ "NAME": "Brian", "READ_COUNT": 400 }, { "NAME": "Pedersen", "READ_COUNT": 100 }] }] }
Converting from the input format to the output format can be achieved with a few lines of code:
// Convert input Json string to a dynamic object dynamic input = JsonConvert.DeserializeObject(myQueueItem); // Create a dynamic output object dynamic output = new ExpandoObject(); output.table = "USER_COUNT"; output.users = new dynamic[1]; output.users[0] = new ExpandoObject(); output.users[0].uid = input.username; output.users[0].rows = new dynamic[input.attributes.attribute.Count]; int ac = 0; foreach (var inputAttribute in input.attributes.attribute) { var row = output.users[0].rows[ac] = new ExpandoObject(); row.NAME = inputAttribute.name; row.READ_COUNT = inputAttribute.count; ac++; } // Serialize the dynamic output object to a string string outputJson = JsonConvert.SerializeObject(output);
I’ll try to further explain what happens. The Newtonsoft.Json DeserializeObject() method takes a json string and converts it to a dynamic object.
The output Json is created by creating a new dynamic object of the type ExpandoObject(). With dynamic ExpandoObjects we can create properties on the fly, like so:
// Create a dynamic output object dynamic output = new ExpandoObject(); // Create a new property called "table" with the value "USER_COUNT" output.table = "USER_COUNT";
This would, when serialized to a Json, create the following output:
{ "table": "USER_COUNT" }
To create an array of objects, you need to first create a new dynamic array and then assign an ExpandoObject to the position in the array:
// Create a dynamic output object dynamic output = new ExpandoObject(); // Create a new array called "users" output.users = new dynamic[1]; // An an object to the "users" array output.users[0] = new ExpandoObject(); // Create a new property "uid" in the "users" array output.users[0].uid = input.username;
This generates the following Json output:
{ "users": [{ "uid": "someuser@somewhere.com" }] }
MORE TO READ:
- Dynamic in C# 4.0: Introducing the ExpandoObject by Alexandra Rusina, Microsoft
- Json.Net by Newtonsoft
- What’s the difference between dynamic(C# 4) and var? from StackOverflow
