How to Convert JSON to an object in C#
By Tan Lee Published on Feb 05, 2025 276
You can achieve this using different libraries, with the two most common being:
- Use
JsonSerializer.Deserialize()
from the built-inSystem.Text.Json
library. - Use
JsonConvert.DeserializeObject()
from theNewtonsoft.Json
package.
Suppose you have the following JSON string representing a book:
1 2 3 4 5 |
{ "Title" : "The Great Gatsby" , "Pages" : 218, "IsAvailable" : true } |
You need a corresponding C# class:
1 2 3 4 5 6 |
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.
1 2 3 4 5 6 |
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:
1 |
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:
1 2 3 4 5 |
{ "title" : "The Great Gatsby" , "pages" : 218, "isAvailable" : true } |
You can use the PropertyNameCaseInsensitive
setting to handle this case:
1 2 3 4 5 6 7 8 9 |
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:
1 |
Install-Package Newtonsoft.Json |
Then, you can deserialize the JSON string as follows:
1 2 3 4 5 |
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:
1 |
The Great Gatsby has 218 pages. Is it available? True. |
Change Deserialization with JsonSerializerSettings
If your JSON contains snake-cased property names, like this:
1 2 3 4 5 |
{ "title" : "The Great Gatsby" , "pages" : 218, "is_available" : true } |
You can configure the deserialization with JsonSerializerSettings
and a SnakeCaseNamingStrategy
to handle it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.
- Serialize and Deserialize a Multidimensional Array in JSON using C#
- JSON object contains a trailing comma at the end which is not supported
- How to use JsonDocument to read JSON in C#
- How to use JsonExtensionData in C#
- Serialize a tuple to JSON in C#
- Deserialize JSON using different property names in C#
- Deserialize JSON to a derived type in C#
- Deserialize JSON to a dictionary in C#