Header Ads

C#.NET: JSON Object Mapper

JSON (JavaScript Object Notation) is one of the most commonly used data passing format used in Web API(s) development. Parsing & manipulating complex JSON objects become quite the headache especially with array parsing technique. JSON objects parsing using C#.NET is on the other hand is quite easy and 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.


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.

Download Now!

Let's begin now.

1) Create a new C#.NET Console Application project and name it "JSONMapper".  
2) Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".
3) Install Newtonsoft.Json NuGet Package i.e.


4) I am using following sample JSON data i.e.


Let's breakdown this sample JSON in order to create out 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.

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.

...

    public class ItemObj
    {
        #region Properties

        /// <summary>
        /// Gets or sets ID property.
        /// </summary>
        public string id { get; set; } = string.Empty;

        /// <summary>
        /// Gets or sets label property.
        /// </summary>
        public string label { get; set; } = string.Empty;

        #endregion
    }

...

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.

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.

...

    public class MenuObj
    {
        #region Properties

        /// <summary>
        /// Gets or sets header property.
        /// </summary>
        public string header { get; set; } = string.Empty;

        /// <summary>
        /// Gets or sets label property.
        /// </summary>
        public List<ItemObj> items { get; set; } = new List<ItemObj>();

        #endregion
    }

...

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.

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.

...

    public class JSONMapperObj
    {
        #region Properties

        /// <summary>
        /// Gets or sets menu property.
        /// </summary>
        public MenuObj menu { get; set; } = new MenuObj();

        #endregion
    }

...

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.

8) Now, open "Program.cs" file and add following lines of code i.e.

...

                // Initialization.
                JSONMapperObj jsonObj = new JSONMapperObj();

                // Prepare sample Data.
...
                // Serialize Data.
                string data = JsonConvert.SerializeObject(jsonObj);
...
                // Deserialize Data.
                JSONMapperObj targetData = JsonConvert.DeserializeObject<JSONMapperObj>(data.ToString());

...

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.

9) Now, execute the project and you will be able to see  following output  i.e.


You can validate your JSON string using online JSON validator tool like JSONLint or any of your choice.

Conclusion

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

3 comments:

  1. Great article explaining how to get JSON objects. I used to do this by hand also but then found automated ways which were a huge help for really big and complicated JSON.

    ReplyDelete
    Replies
    1. Thank you for your feedback, would love to know your automated tools for JSON object mapper

      Delete
  2. watch your pinoy tv channenl online in hd. All the pinoy lambingan tambayan tv replays in hd.

    ReplyDelete