How to Convert JSON to an object in C#

By FoxLearn 2/5/2025 6:58:03 AM   129
In C#, converting a JSON string into an object is known as deserialization.

You can achieve this using different libraries, with the two most common being:

  • Use JsonSerializer.Deserialize() from the built-in System.Text.Json library.
  • Use JsonConvert.DeserializeObject() from the Newtonsoft.Json package.

Suppose you have the following JSON string representing a book:

{
  "Title": "The Great Gatsby",
  "Pages": 218,
  "IsAvailable": true
}

You need a corresponding C# class:

public class Book
{
    public string Title { get; set; }
    public int Pages { get; set; }
    public bool IsAvailable { get; set; }
}

Using JsonSerializer.Deserialize() (in System.Text.Json)

To deserialize using the built-in JsonSerializer.Deserialize(), you provide the JSON string and specify the type of object.

using System.Text.Json;

var bookJson = "{ \"Title\": \"The Great Gatsby\", \"Pages\": 218, \"IsAvailable\": true }";
var book = JsonSerializer.Deserialize<Book>(bookJson);

Console.WriteLine($"{book.Title} has {book.Pages} pages. Is it available? {book.IsAvailable}.");

This will deserialize the JSON string to a Book object and output:

The Great Gatsby has 218 pages. Is it available? True.

Change Deserialization with JsonSerializerOptions

If your JSON uses camel-casing, you can adjust the deserialization process with JsonSerializerOptions.

For example, consider this camel-cased JSON:

{
  "title": "The Great Gatsby",
  "pages": 218,
  "isAvailable": true
}

You can use the PropertyNameCaseInsensitive setting to handle this case:

using System.Text.Json;

var jsonOptions = new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true
};
var book = JsonSerializer.Deserialize<Book>(bookJson, jsonOptions);

Console.WriteLine($"{book.Title} has {book.Pages} pages. Is it available? {book.IsAvailable}.");

Using JsonConvert.DeserializeObject() (from Newtonsoft)

To use Newtonsoft.Json, first install the package via NuGet:

Install-Package Newtonsoft.Json

Then, you can deserialize the JSON string as follows:

using Newtonsoft.Json;

var book = JsonConvert.DeserializeObject<Book>(bookJson);

Console.WriteLine($"{book.Title} has {book.Pages} pages. Is it available? {book.IsAvailable}.");

This deserializes the JSON string to a Book object and outputs:

The Great Gatsby has 218 pages. Is it available? True.

Change Deserialization with JsonSerializerSettings

If your JSON contains snake-cased property names, like this:

{
  "title": "The Great Gatsby",
  "pages": 218,
  "is_available": true
}

You can configure the deserialization with JsonSerializerSettings and a SnakeCaseNamingStrategy to handle it:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

var settings = new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver()
    {
        NamingStrategy = new SnakeCaseNamingStrategy()
    }
};
var book = JsonConvert.DeserializeObject<Book>(bookJson, settings);

Console.WriteLine($"{book.Title} has {book.Pages} pages. Is it available? {book.IsAvailable}.");

This allows you to properly handle snake-cased properties during deserialization.

Whether you use the built-in System.Text.Json or Newtonsoft.Json, deserialization in C# can be done efficiently with just a few lines of code.