C#.NET - JSON Object Mapper

JSON (JavaScript Object Notation) is one of the most commonly used data passing formats used in Web API(s) development. Parsing & manipulating complex JSON objects becomes quite the headache especially with array parsing technique. JSON objects parsing using C#.NET is on the other hand is quit simple, all you have to do is to create your target JSON object mapper and then deserialize the JSON string/object.
 
Today, I shall be demonstrating creation of a complex JSON object mapper using C#.NET technology Console Application.
 
C#.NET - JSON Object Mapper
 
Prerequisites
 
Following are some prerequisites before you proceed any further in this tutorial,
  1. Knowledge of JSON string.
  2. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2019 Professional.
 
 
Let's begin now.
 
Step 1
 
Create a new C#.NET Console Application project and name it "JSONMapper".  
 
Step 2
 
Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".
 
Step 3
 
Install Newtonsoft.Json NuGet Package i.e.
 
C#.NET - JSON Object Mapper
 
Step 4
 
I am using the  following sample JSON data i.e.
 
C#.NET - JSON Object Mapper
 
Let's breakdown this sample JSON in order to create out Object mapper.
 
C#.NET - JSON Object Mapper
 
In the above snippet, 1, 2, 5, & 6 points indicate the start & end of the JSON object and any list object inside. Point 4 indicates names of the object which should be used exactly as they are mentioned that includes case sensitivity as well, these names will be used during the creation of our JSON mapper object. Point 3 indicates start of the object and point 7 indicates value of the object which could be a list, integer value, bool, string etc. Point 8 highlights a single item of the list object which needs to be constructed according to the structure provided in point 8.
 
Step 5
 
So, based on the above understanding of our sample JSON, create list item structure first, by creating a new "Objects\ItemObj.cs" file and replace following code in it i.e.
  1. ...  
  2.   
  3.     public class ItemObj  
  4.     {  
  5.         #region Properties  
  6.   
  7.         /// <summary>  
  8.         /// Gets or sets ID property.  
  9.         /// </summary>  
  10.         public string id { getset; } = string.Empty;  
  11.   
  12.         /// <summary>  
  13.         /// Gets or sets label property.  
  14.         /// </summary>  
  15.         public string label { getset; } = string.Empty;  
  16.  
  17.         #endregion  
  18.     }  
  19.   
  20. ... 
In the above code, I have created the list item object structure according to my sample JSON, notice that the names of the properties are exactly according to the JSON structure with case sensitivity.
 
Step 6
 
Now, create a menu object class which contains a string object and a list object of type ItemObj that I have created in the previous step, so, create a new "Objects\MenuObj.cs" file and replace following code in it i.e.
  1. ...  
  2.   
  3.     public class MenuObj  
  4.     {  
  5.         #region Properties  
  6.   
  7.         /// <summary>  
  8.         /// Gets or sets header property.  
  9.         /// </summary>  
  10.         public string header { getset; } = string.Empty;  
  11.   
  12.         /// <summary>  
  13.         /// Gets or sets label property.  
  14.         /// </summary>  
  15.         public List<ItemObj> items { getset; } = new List<ItemObj>();  
  16.  
  17.         #endregion  
  18.     }  
  19.   
  20. ... 
In the above code, you can see that the menu object class contains the properties according to the sample JSON with the names of the properties which are exactly according to the JSON structure with case sensitivity.
 
Step 7
 
For the final object, we need to create the main object which holds the complete structure of the JSON as according to our sample JSON file, there is still one object (the main object) whose object class with provided name has not yet been created, so, create "Objects\JSONMapperObj.cs" file and replace it with following code i.e.
  1. ...  
  2.   
  3.     public class JSONMapperObj  
  4.     {  
  5.         #region Properties  
  6.   
  7.         /// <summary>  
  8.         /// Gets or sets menu property.  
  9.         /// </summary>  
  10.         public MenuObj menu { getset; } = new MenuObj();  
  11.  
  12.         #endregion  
  13.     }  
  14.   
  15. ... 
From the above, you can verify that the most top object mentioned in our sample JSON is "menu" which holds the entire JSON objects, so, I have created the main object according to the sample JSON with the names of the properties which are exactly according to the JSON structure with case sensitivity.
 
Step 8
 
Now, open "Program.cs" file and add following lines of code i.e.
  1. ...  
  2.   
  3.                 // Initialization.  
  4.                 JSONMapperObj jsonObj = new JSONMapperObj();  
  5.   
  6.                 // Prepare sample Data.  
  7. ...  
  8.                 // Serialize Data.  
  9.                 string data = JsonConvert.SerializeObject(jsonObj);  
  10. ...  
  11.                 // Deserialize Data.  
  12.                 JSONMapperObj targetData = JsonConvert.DeserializeObject<JSONMapperObj>(data);  
  13.   
  14. ... 
The above code simply prepare the sample JSON data according to the sample JSON, the serialize the JSON object to pass the data over either web API or on network, then target data is deserialized and can be manipulated easily.
 
Step 9
 
Now, execute the project and you will be able to see  following output  i.e.
 
C#.NET - JSON Object Mapper
 
You can validate your JSON string using online JSON validator tool like JSONLint or any of your choice.
 

Conclusion

 
In this article, you learned to create a complex JSON object mapper using C#.NET technology Console Application. You also learned to break  down your target JSON and then create required JSON mapper objects with the names of the properties exactly according to the JSON structure with case sensitivity. You learned to serialize and deserialize the JSON string & object to pass it over the network or to share it via web API. Finally, you learned about the tools you need to validate your JSON data.