Deserialization is the process of taking a string representation of an object and creating that object in memory. JSON is an ever-increasingly popular format to serialize objects into. It has become nearly ubiquitous in API communication. In this article, we will take a look at two ways to execute the JSON deserialization to a POCO class in C# using System.Text.Json and Newtonsoft.Json libraries.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Deserialize JSON to a POCO Class

In the next sections, we will take a look at how we can deserialize a JSON string into an instance of a POCO class object.ย 

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

JSON Deserialization to a POCO Class With System.Text.Json

First, let’s take a look at deserializing with the .NET System.Text.Json library. This library is standard with .NET and all we need to do to be able to use it is to add using System.Text.Json; to the file we are working with.

Let’s start by defining a JSON object in a file:

// JSON/HondaCivic.json
{
  "make": "Honda",
  "model": "Civic",
  "year": 2015,
  "features": [
    "ABS",
    "Automatic locks",
    "Power windows"
  ]
}

Now that we have a serialized object in JSON, we can deserialize it into our POCO Car class:

using System.Text.Json.Serialization;

public class Car
{
   [JsonPropertyName("make")]
   public string Make { get; set; } = default!;
   [JsonPropertyName("model")]
   public string Model { get; set; } = default!;
   [JsonPropertyName("year")]
   public int Year { get; set; } = default!;
   [JsonPropertyName("features")]
   public IEnumerable<string> Features { get; set; } = default!;
}

In this class, we create class members to match the fields in the expected JSON object we are trying to deserialize. Additionally, we decorate the properties with the JsonPropertyName() attribute. This attribute is only required for System.Text.Json, we donโ€™t have to use it with Newtonsoft.Json.

Next, let’s modify the Program class:

public static void Main()
{
   // Read JSON file. This could also be a DB or API response.
   var jsonStr = File.ReadAllText("JSON/HondaCivic.json");

   // Deserialize to get an object of type Car
   DeserializedJsonCar = JsonSerializer.Deserialize<Car>(jsonStr)!;
}

We read JSON from a file using the ReadAllText method, and deserialize it to the DeserializeJsonCar property using the JsonSerializer.Deserialize method. Note that the source of the JSON can be more than a text file. JSON can come from a database or an API response.

That’s it, we are done! We have recovered the state of the Car in the JSON file by deserializing the JSON into an object in memory.

In this article, we deserialize a simple JSON object. But if you want to learn more about the complex deserialization, you can read this article

JSON Deserialization to a POCO Class With Newtonsoft.Json

Next, we are going to take a look at the same example but this time we will use Newtonsoft.Json. This article explains the difference between these two libraries in greater detail.

First, let’s bring in Newtonsoft.Json as a dependency. We can do this by installing the Newtonsoft.Json NuGet package via the Visual Studio NuGet Package Manager Console:

Install-Package Newtonsoft.Json

Once this dependency is installed we can import Newtonsoft.Json into our code.

Now, we are ready to deserialize our JSON object into a Car object:

public static void Main()
{
    var jsonStr = File.ReadAllText("JSON/HondaCivic.json");

    NewtonsoftDeserializedJsonCar = JsonConvert.DeserializeObject<Car>(jsonStr)!;
}

We can see that the process with Newtonsoft.Json is quite similar to the one with System.Text.Json. Just, in this case, we are using the JsonConvert class to call the Deserialize method.

Conclusion

In summary, we have covered two options to deserialize JSON into a POCO class in C#. Even though we’ve deserialized JSON from a file, the source of the JSON can also be a database or API. Using JSON is a great way to transfer data from the backend to a web app or desktop application. Moreover, we can keep the state of the object intact.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!