Converting One JSON to Another in C# Using Newtonsoft and Dynamic ExpandoObject
By FoxLearn 1/9/2025 2:58:17 AM 56
Prior to the introduction of C#'s dynamic
type and ExpandoObject
, developers would typically serialize an input JSON into POCO (Plain Old CLR Object) model classes, and then use a Factory class to convert it to another set of POCO classes before serializing the output back into JSON.
With the advent of dynamic
and ExpandoObject
, a new, more flexible approach became available. You can deserialize the input JSON into a dynamic
object and then transform it into another dynamic
object, which can be serialized into the desired output format.
Let's explore an example with the following input and output JSON formats:
Input format:
[ { "id": 101, "name": "Laptop", "category": "Electronics", "price": 1000 }, { "id": 102, "name": "Smartphone", "category": "Electronics", "price": 800 }, { "id": 103, "name": "T-shirt", "category": "Apparel", "price": 25 }, { "id": 104, "name": "Jeans", "category": "Apparel", "price": 40 } ]
You want to transform this input JSON into an output JSON that groups products by category, like this:
{ "Electronics": [ { "id": 101, "name": "Laptop", "price": 1000 }, { "id": 102, "name": "Smartphone", "price": 800 } ], "Apparel": [ { "id": 103, "name": "T-shirt", "price": 25 }, { "id": 104, "name": "Jeans", "price": 40 } ] }
Converting Input JSON to Output JSON
The goal here is to group the products by category. You can achieve this using C#'s dynamic
and ExpandoObject
as follows:
// Deserialize the input JSON string into a dynamic object dynamic input = JsonConvert.DeserializeObject(myQueueItem); // Create a new dynamic object for the output dynamic output = new ExpandoObject(); // Group products by category foreach (var product in input) { string category = product.category.ToString(); // Initialize category array if it doesn't exist if (output[category] == null) { output[category] = new List<dynamic>(); } // Create a new product object for the output dynamic productOutput = new ExpandoObject(); productOutput.id = product.id; productOutput.name = product.name; productOutput.price = product.price; // Add the product to the corresponding category output[category].Add(productOutput); } // Serialize the dynamic output object to JSON string outputJson = JsonConvert.SerializeObject(output, Formatting.Indented);
The JsonConvert.DeserializeObject()
method is used to deserialize the input JSON string into a dynamic
object. This gives you the flexibility to access each product directly without predefined model classes.
dynamic input = JsonConvert.DeserializeObject(myQueueItem);
The output is created as a new ExpandoObject
, which allows us to dynamically add properties (in this case, categories like "Electronics", "Apparel", etc.) as we go.
We iterate through each product in the input JSON, group them by category, and add them to the respective category in the output.
dynamic output = new ExpandoObject(); foreach (var product in input) { string category = product.category.ToString(); }
For each product, we check if the category already exists in the output object. If it doesn’t exist, we initialize an empty list for that category.
Then, we create a new ExpandoObject
for each product, containing only the id
, name
, and price
, and add it to the corresponding category array.
if (output[category] == null) { output[category] = new List<dynamic>(); } dynamic productOutput = new ExpandoObject(); productOutput.id = product.id; productOutput.name = product.name; productOutput.price = product.price; output[category].Add(productOutput);
After transforming the input JSON into the desired structure, we use JsonConvert.SerializeObject()
to serialize the dynamic
object back to a formatted JSON string.
string outputJson = JsonConvert.SerializeObject(output, Formatting.Indented);
The dynamic
type and ExpandoObject
allow us to work with flexible, nested data structures that may not be known at compile time, making the approach ideal for dealing with loosely defined or changing JSON formats.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#