Deserialize JSON into C# Object/Type Dynamically using System.Text.Json

SystemTextJson Deserialize JSON into C Object

In this article, we will understand how to perform System.Text.Json Deserialize JSON into C# Object/Type Dynamically.

In our previous article, we already looked at how to Deserialize JSON into C# Object/Type dynamically using NewtonSoft(JSON.NET).

Today in this article, we will cover below aspects,

As we know System.Text.Json will be the default serializer and deserializer for ASP.NET Core 3.1 or ASP.NET 6.0 onwards.

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

System.Text.Json -Deserialize JSON into C# object dynamically with know type

We shall be using the below sample to de-serialize JSON using System.Text.Json

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

Generic method for JSON type Conversion to Any Type or Object

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

Here we are generating Generic <T> from the given JSON string.

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

SystemTextJson Deserialize JSON into C Object

Where GetJsonGenericType is just a wrapper method around JsonConvert.DeserializeObject

Client-side code

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

Client-side code for the scenario I will be as below,

UserDetails userDetails = GetJsonGenericType<Userdetails>(strJSON);

OutPut:

SystemTextJson Deserialize JSON into C ObjectType Dynamically

OR

You can use any other type,

EmployeeDetails emplyeeDetails = GetJsonGenericType<EmployeeDetails>(strJSON);

Deserialize JSON into C# List or Array object dynamically with known 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)

SystemTextJson Deserialize JSON into C ObjectType Dynamically

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.

Above we are creating a wrapper method just to reduce the impact of change of Serializer. Like previously my code had references for Netwtonsoft at 1000 places.

To switch to a new serializer, I had to change to the references everywhere. Having wrapper , will neeed you to change only at one place.

Do you see any other light-weight serializer coming soon ?? 🙂

System.Text.Json – 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 “System.Text.Json Deserialize JSON into C# Object/Type Dynamically

  1. Sorry – but I don’t get it.
    What is the benefit of using

    UserDetails userDetails = GetJsonGenericType(json);

    instead of

    UserDetails userDetails = JsonSerializer.Deserialze(json);

    ?

    1. Hey Thomas – Thanks for your query. I agree with you. You can very much use it directly.

      Idea here was just to make additional utility method and centralize the logic.

      Recently JSON.NET was replaced by System.Text.Json. Having this method would required just one change. Again this is just an example.

Leave a Reply

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