Blog post
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Work with JSON in C# — System.Text.Json and Newtonsoft.Json compared, serialization options, custom converters, and ASP.NET integration.
Shashank Jain
Author


Article
Two Main Libraries
C# has two major JSON libraries: System.Text.Json (built into .NET Core 3+, fast, minimal allocations) and Newtonsoft.Json/Json.NET (older, feature-rich, still widely used).
System.Text.Json (Modern)
using System.Text.Json;
using System.Text.Json.Serialization;
public record User(int Id, string Name, string Email);
// Serialize
var user = new User(1, "Alice", "alice@example.com");
string json = JsonSerializer.Serialize(user);
// {"Id":1,"Name":"Alice","Email":"alice@example.com"}
// Deserialize
var decoded = JsonSerializer.Deserialize<User>(json);
Console.WriteLine(decoded?.Name); // Alice
// With options
var options = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string pretty = JsonSerializer.Serialize(user, options);Newtonsoft.Json (Json.NET)
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
var settings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
};
// Serialize
string json = JsonConvert.SerializeObject(user, settings);
// Deserialize
var decoded = JsonConvert.DeserializeObject<User>(json, settings);
// Dynamic / JObject
var jobject = JObject.Parse(json);
string name = (string)jobject["name"];Annotations
// System.Text.Json
public class Product {
[JsonPropertyName("product_id")]
public int Id { get; set; }
[JsonIgnore]
public string InternalCode { get; set; }
[JsonInclude]
public decimal Price { get; set; }
}
// Newtonsoft
public class Product {
[JsonProperty("product_id")]
public int Id { get; set; }
[JsonIgnore]
public string InternalCode { get; set; }
}System.Text.Json vs Newtonsoft
| Feature | System.Text.Json | Newtonsoft |
|---|---|---|
| Performance | 2-3x faster | Slower |
| .NET built-in | Yes (.NET Core 3+) | NuGet package |
| Nullable ref types | Full support | Partial |
| Polymorphism | .NET 7+ | TypeNameHandling |
| Dynamic JSON | JsonNode / JsonElement | JObject / JToken |
| Feature completeness | Growing | Very rich |
ASP.NET Core Integration
// Program.cs — configure JSON options
builder.Services.AddControllers().AddJsonOptions(opts => {
opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
// Switch to Newtonsoft in ASP.NET Core
builder.Services.AddControllers().AddNewtonsoftJson(opts => {
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});FAQ
Should new .NET projects use System.Text.Json or Newtonsoft?
Use System.Text.Json for new .NET 6+ projects — it is faster, built-in, and improving rapidly. Migrate existing projects from Newtonsoft gradually. Stick with Newtonsoft if you rely on features System.Text.Json does not yet support.
How do I handle polymorphic JSON in System.Text.Json?
.NET 7+ has built-in polymorphism via JsonDerivedType attributes. Earlier versions require a custom JsonConverter. Newtonsoft supports TypeNameHandling.Auto but it is a security risk for untrusted input.
How do I parse dynamic JSON in C#?
With System.Text.Json, use JsonDocument.Parse() or JsonNode.Parse(). With Newtonsoft, use JObject.Parse() or JToken.Parse(). These let you navigate the JSON tree without a class definition.
How do I ignore null values when serializing?
System.Text.Json: set DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull in JsonSerializerOptions. Newtonsoft: set NullValueHandling = NullValueHandling.Ignore in JsonSerializerSettings.
How do I serialize enums as strings in C#?
System.Text.Json: add new JsonStringEnumConverter() to options.Converters. Newtonsoft: add StringEnumConverter to settings.Converters, or use [JsonConverter(typeof(StringEnumConverter))] on the property.
Keep reading
Recent blogs

Jun 14, 2026
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Serialize and deserialize JSON in C# using System.Text.Json and Newtonsoft.Json with practical examples.

Jun 14, 2026
JSON to Markdown Table: Convert JSON Arrays Instantly
Convert JSON arrays to Markdown tables in JavaScript, Python, and with the free online tool.

Jun 14, 2026
JSON in TypeScript: Type-Safe Parsing and Validation
Stop using any for JSON in TypeScript — use Zod, type guards, and generics for fully type-safe parsing.