jsondecode.com logo

HomeChevronBlogChevronJSON in C#: System.Text.Json and Newtonsoft Complete Guide

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.

author

Shashank Jain

Author

14/06/20261 minute 42 seconds read
JSON in C#: System.Text.Json and Newtonsoft Complete GuideJSON in C#: System.Text.Json and Newtonsoft Complete Guide

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

FeatureSystem.Text.JsonNewtonsoft
Performance2-3x fasterSlower
.NET built-inYes (.NET Core 3+)NuGet package
Nullable ref typesFull supportPartial
Polymorphism.NET 7+TypeNameHandling
Dynamic JSONJsonNode / JsonElementJObject / JToken
Feature completenessGrowingVery 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

View all

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.