C# – Convert JSON to an object

Converting a JSON string to an object is referred to as deserialization. You can do this with a JSON serializer. There are two primary options:

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

I’ll show examples by deserializing the following Movie JSON to a Movie object:

Movie JSON:

{
  "Title": "Jurassic Park",
  "RuntimeMinutes": 127,
  "IsGood": true
}Code language: JSON / JSON with Comments (json)

Movie class (properties match JSON):

public class Movie
{
    public string Title { get; set; }
    public int RuntimeMinutes { get; set; }
    public bool IsGood { get; set; }
}
Code language: C# (cs)

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

To deserialize with the built-in JsonSerializer.Deserialize(), pass in the JSON string and specify the type of object you want. Here’s an example:

using System.Text.Json;

var movie = JsonSerializer.Deserialize<Movie>(movieJson);

Console.WriteLine($"{movie.Title} has a runtime of {movie.RuntimeMinutes}. Is it good? {movie.IsGood}.");
Code language: C# (cs)

This deserializes the JSON string to a Movie object and outputs the following:

Jurassic Park has a runtime of 127. Is it good? True.Code language: plaintext (plaintext)

Change deserialization with JsonSerializerOptions

You can pass in a JsonSerializerOptions object to control deserialization to get the results you want. For example, let’s say you’re dealing with camel-cased JSON property names, like this:

{
  "title": "Jurassic Park",
  "runtimeMinutes": 127,
  "isGood": true
}Code language: JSON / JSON with Comments (json)

This is a common problem because JSON often uses camel-casing while C# uses pascal-casing by convention. One way to deal with that is to use the PropertyNameCaseInsensitive setting. Here’s an example:

using System.Text.Json;

var jsonOptions = new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true
};
var movie = JsonSerializer.Deserialize<Movie>(movieJson, jsonOptions);
Code language: C# (cs)

For customizing deserialization further, you can write a custom JSON converter.

Using JsonConvert.DeserializeObject() (from Newtonsoft)

To use Newtonsoft, first install the Newtonsoft.Json package (via Package Manager Console):

Install-Package Newtonsoft.Json
Code language: PowerShell (powershell)

Now use JsonConvert.DeserializeObject() by passing in a JSON string and specifying the type to deserialize to. Here’s an example:

using Newtonsoft.Json;

var movie = JsonConvert.DeserializeObject<Movie>(movieJson);

Console.WriteLine($"{movie.Title} has a runtime of {movie.RuntimeMinutes}. Is it good? {movie.IsGood}.");
Code language: C# (cs)

This deserializes the JSON string to a Movie object. It outputs the following:

Jurassic Park has a runtime of 127. Is it good? True.Code language: plaintext (plaintext)

Change deserialization with JsonSerializerSettings

You can pass in a JsonSerializerSettings object to control deserialization. Here’s an example. Let’s say you’re dealing with JSON containing snake-cased property names, like this:

{
  "title": "Jurassic Park",
  "runtime_minutes": 127,
  "is_good": true
}Code language: JSON / JSON with Comments (json)

One way to deal with this is using the SnakeCaseNamingStrategy, like this:

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

var settings = new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver()
    {
        NamingStrategy = new SnakeCaseNamingStrategy()
    }
};
var movie = JsonConvert.DeserializeObject<Movie>(movieJson, settings);
Code language: C# (cs)