Deserialize n-level(multi level) JSON data to C# custom object

Ever wondered how to deserialize a multi level(nested) JSON data where you are not sure of the level. It my be upto level 5 or upto n level.

I faced a similar situation where I have to fetch the product information from an API, exposed by an eCommerce site. And the JSON I got is of a multi level nested JSON.

I searched a lot in Google on how to hold that JSON data into a custom C# List of objects, but found no help. After a long research and trying my hands here and there I found the best solution.



Here is the JSO format I was getting from API.
{
   "Categories":[
      {
         "Code":"2984",
         "Name":"Baby",
         "Children":[
            {
               "Code":"100978",
               "Name":"Christening & Gifts",
               "Children":[
                  {
                     "Code":"100980",
                     "Name":"Baby Jewellery"
                  },
                  {
                     "Code":"100981",
                     "Name":"Ornaments"
                  },
                  {
                     "Code":"121628",
                     "Name":"Gift Baskets"
                  },
                  {
                     "Code":"139760",
                     "Name":"Christening",
                     "Children":[
                        {
                           "Code":"100979",
                           "Name":"Gifts"
                        },
                        {
                           "Code":"139764",
                           "Name":"Silverware"
                        },
                        {
                           "Code":"139765",
                           "Name":"Other Christening"
                        }
                     ]
                  },
                  {
                     "Code":"32871",
                     "Name":"Other Gifts"
                  }
               ]
            }
         ]
      },
      {
         "Code":"100982",
         "Name":"Baby Carriers/Backpacks"
      },
      {
         "Code":"1261",
         "Name":"Other Baby"
      },
      {
         "Code":"134282",
         "Name":"Walkers"
      }
   ]
}
Here you can see there is 3 level of children but this can go upto n-level. So the question is how your C# object structure should be to hold this type of JSO data.

There is a free tool available that will take your JSON and give the respective C# object structure. Here it is. http://json2csharp.com/

This tool give me the below class structure.
public class Child3
{
    public string Code { get; set; }
    public string Name { get; set; }
}

public class Child2
{
    public string Code { get; set; }
    public string Name { get; set; }
    public List<Child3> Children { get; set; }
}

public class Child
{
    public string Code { get; set; }
    public string Name { get; set; }
    public List<Child2> Children { get; set; }
}

public class Category
{
    public string Code { get; set; }
    public string Name { get; set; }
    public List<Child> Children { get; set; }
}

public class RootObject
{
    public List<Category> Categories { get; set; }
}
I am sure this is not the optimum solution to the problem because it can hold data only up to 3 level. If the hierarchy goes on increasing then this will fail to perform.



So if you are looking for an optimum solution and here it is.
public class MyRootCategory
  {
      public Node[] Categories { get; set; }
  }
  
  public class Node
  {
      public string Code { get; set; }
      public string Name { get; set; }
      public Node[] Children { get; set; }
  }
This class structure can handle n-level hierarchy. And is the optimum solution we are looking for.

The code below will deserialize the json string to an object of type MyRootCategory
  var objJSON = JObject.Parse(result);
  var objResponse = objJSON["Response"].ToString();

  MyRootCategory categoryTree = JsonConvert.DeserializeObject<MyRootCategory>(objResponse);
If you are looking for, how to save this n-level list to database by iterating then stay tuned to my next article

Till then Happy Codding....

No comments:

Post a Comment