C# has two main libraries for working with JSON: System.Text.Json, native to .NET since version 3.0, and Newtonsoft.Json (also known as Json.NET), a third-party library that’s been around much longer and is still extremely popular. This post covers both, and when it makes sense to use each one.
System.Text.Json: native, no external dependency
Since it ships with .NET, there’s nothing to install via NuGet to use it.
Deserialization (JSON to object)
// csharp
using System.Text.Json;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
string json = "{\"Name\": \"Ana\", \"Age\": 30}";
Person person = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine(person.Name); // Ana
Serialization (object to JSON)
// csharp
Person person = new Person { Name = "Ana", Age = 30 };
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json); // {"Name":"Ana","Age":30}
Handling case sensitivity
By default, System.Text.Json is case-sensitive when matching JSON keys to class properties. If the JSON uses camelCase (common in APIs) but your C# class uses PascalCase (C#’s convention), you need to configure this explicitly.
// csharp
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
Person person = JsonSerializer.Deserialize<Person>(json, options);
To generate JSON in camelCase during serialization (useful when your API will be consumed by JavaScript):
// csharp
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
string json = JsonSerializer.Serialize(person, options);
// {"name":"Ana","age":30}
Newtonsoft.Json (Json.NET): more features, more mature
Still widely used, especially in legacy projects or ones that need features System.Text.Json doesn’t fully cover (like certain advanced serialization customizations).
Install via NuGet:
dotnet add package Newtonsoft.Json
Deserialization
// csharp
using Newtonsoft.Json;
Person person = JsonConvert.DeserializeObject<Person>(json);
Serialization
// csharp
string json = JsonConvert.SerializeObject(person);
The API is conceptually similar (static methods that take/return a string), but with different names: JsonConvert.DeserializeObject instead of JsonSerializer.Deserialize, and JsonConvert.SerializeObject instead of JsonSerializer.Serialize.
Case insensitive by default
An important difference: Newtonsoft.Json already matches keys case-insensitively by default, with no extra configuration needed. This tends to be more convenient day to day, but it also means different behavior from System.Text.Json if you migrate from one library to the other without adjusting for it.
Formatting the output JSON (pretty print)
Both libraries configure readable formatting in a similar way.
// csharp
// System.Text.Json
var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(person, options);
// Newtonsoft.Json
string json2 = JsonConvert.SerializeObject(person, Formatting.Indented);
Which one should you choose?
| System.Text.Json | Newtonsoft.Json | |
|---|---|---|
| External dependency | No (native to .NET) | Yes (via NuGet) |
| Performance | Generally faster | Slightly slower |
| Case sensitivity | Sensitive by default | Insensitive by default |
| Advanced features | Covers most cases | More mature, more customization options |
| Best for | New projects, modern .NET | Legacy projects, advanced customization cases |
For new projects, the general recommendation is to start with System.Text.Json, since it avoids an external dependency and covers the vast majority of use cases. Newtonsoft.Json still makes sense for legacy projects or when you need some specific feature not covered by the native library.
Quick checklist
- Do you know which library your project already uses before adding new code (avoid mixing the two unnecessarily)?
- Configured
PropertyNameCaseInsensitiveinSystem.Text.Jsonif the JSON comes in camelCase and your class is in PascalCase? - Considered whether you really need the extra Newtonsoft.Json dependency, or if the native
System.Text.Jsonalready solves it?
To quickly validate a JSON before mapping it to a C# class, paste it into JSON Validator.