Deserialize JSON into C# Object/Type dynamically with and without class

Deserialize JSON into TypeObject dynamically

Today in this article, we shall see how Deserialize JSON into C# Object dynamically with and without class.

If you are looking for better a way to call a generic method when the type parameter isn’t known at compile-time, but want to obtain it dynamically at runtime then the below logic could be very useful.

So overall in this article, we shall see below aspects using NewtonSoft. JSON. If you need to see the same using System.Text.JSON, please visit this article,

The below logic helps to convert JSON objects into a type of your choice dynamically. Here I am using generics <T> to denote the same.

Deserialize JSON into C# object dynamically with know type

I have simple JSON as shown below,

{

"ID":"1001",
"Name":"ABCD",
"City":"City1",
"Country":"USA"

}

Client-side code for Scenario I will be as below,

UserDetails userDetails = GetJsonGenericType<Userdetails>(strJSON);

Output:

Deserialize JSON into TypeObject dynamically

Below logic will generate type dynamically which can be cast to the appropriate type as needed.

Where GetJsonGenericType is just a wrapper method around JsonConvert.DeserializeObject

Note: You can use direct JsonConvert.DeserializeObject method if you don’t want a wrapper.

Let’s use the below method to convert this JSON into type dynamically,

  
 private static T GetJsonGenericType<T>(string strJSON)
        {
            return JsonConvert.DeserializeObject<T>(strJSON);
        }

Deserialize JSON into C Object

Deserialize JSON into C# List or Array object dynamically with know type

Let’s consider a little complex JSON array example,

[
  {
    "ID": "1001",
    "Name": "ABCD",
    "City": "City1",
    "Country": "USA"
  },
  {
    "ID": "1002",
    "Name": "PQRS",
    "City": "City2",
    "Country": "INDIA"
  }
]

Client-side code for the second JSON example will be as below,

List<userdetails> userDetails = GetJsonGenericType<List<Userdetails>>(strJSON)

Deserialize JSON into C Object

As you see above, the generic method defined can be used to map to the type of your choice. The details will get mapped based on Key matches and the Value will be set accordingly.

Client-side code

Let’s assume you would like to map the JSON data to a specific type on the client side.

public class UserDetails
         {
             public string ID { get; set; }
             public string Name { get; set; }
             public string City { get; set; }
             public string Country { get; set; }
         }

Deserialize JSON into Dynamic Object without Class

If you do not know your type information then using dynamic you can create the object as below,

var userDetails = GetJsonGenericType<dynamic>(strJSON)

Similarly for JSON with a List or array of data,

var userDetails = GetJsonGenericType<List<dynamic>>(strJSON);

References :

That’s All! Happy Coding!

Do you have any better suggestions for the above? Please sound off in the comments below!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “Deserialize JSON into C# Object dynamically

Leave a Reply

Your email address will not be published. Required fields are marked *