How to Parse JSON in C#
C# has two major JSON libraries: System.Text.Json (built-in, .NET 5+) and Newtonsoft.Json (Json.NET). This guide covers both with typed deserialization, error handling, and real-world patterns.
System.Text.Json (Built-in, Recommended)
using System.Text.Json;
// Define a matching class
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public bool Active { get; set; }
}
// Deserialize JSON string to typed object
string json = """{"name":"Alice","age":30,"active":true}""";
var user = JsonSerializer.Deserialize<User>(json);
Console.WriteLine(user.Name); // Alice
Console.WriteLine(user.Age); // 30
// Serialize object to JSON string
string output = JsonSerializer.Serialize(user);
// '{"Name":"Alice","Age":30,"Active":true}'
// Pretty-print
var options = new JsonSerializerOptions { WriteIndented = true };
string pretty = JsonSerializer.Serialize(user, options);
// Case-insensitive deserialization (default is case-sensitive)
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var user2 = JsonSerializer.Deserialize<User>(json, opts);Newtonsoft.Json (Json.NET)
Install via NuGet: Install-Package Newtonsoft.Json
using Newtonsoft.Json;
// Deserialize to typed class
string json = """{"name":"Alice","age":30,"active":true}""";
var user = JsonConvert.DeserializeObject<User>(json);
Console.WriteLine(user.Name); // Alice
// Serialize to JSON
string output = JsonConvert.SerializeObject(user);
// Pretty-print
string pretty = JsonConvert.SerializeObject(user, Formatting.Indented);
// Deserialize to dynamic (no class needed)
dynamic obj = JsonConvert.DeserializeObject(json);
Console.WriteLine(obj.name); // Alice
// Deserialize a JSON array
string jsonArray = """[{"name":"Alice"},{"name":"Bob"}]""";
var users = JsonConvert.DeserializeObject<List<User>>(jsonArray);Error Handling
using System.Text.Json;
// System.Text.Json
try
{
var user = JsonSerializer.Deserialize<User>(json);
}
catch (JsonException ex)
{
Console.WriteLine($"JSON error: {ex.Message}");
Console.WriteLine($"Line: {ex.LineNumber}, Position: {ex.BytePositionInLine}");
}
// Newtonsoft.Json
try
{
var user = JsonConvert.DeserializeObject<User>(json);
}
catch (JsonReaderException ex)
{
Console.WriteLine($"JSON error: {ex.Message}");
Console.WriteLine($"Line: {ex.LineNumber}");
}
catch (JsonSerializationException ex)
{
Console.WriteLine($"Mapping error: {ex.Message}");
}Parse JSON in Other Languages
FAQ
How do I parse JSON in C#?expand_more
Use System.Text.Json.JsonSerializer.Deserialize<T>(jsonString) (built into .NET). For older projects, use Newtonsoft.Json: JsonConvert.DeserializeObject<T>(jsonString). Both accept a JSON string and return a typed C# object.
What is the difference between System.Text.Json and Newtonsoft.Json in C#?expand_more
System.Text.Json is built into .NET Core 3.0+ and .NET 5+. It is faster and has lower allocations. Newtonsoft.Json (Json.NET) has been around longer, has more features, and is easier to configure for complex scenarios. For new projects on modern .NET, prefer System.Text.Json.
How do I deserialize JSON to a C# class?expand_more
Define a C# class with properties matching the JSON keys, then call JsonSerializer.Deserialize<YourClass>(jsonString). Example: var user = JsonSerializer.Deserialize<User>(json); where User has Name and Age properties.
Part of the JSON Toolkit
Explore All JSON Tools
Free online tools for every JSON task — format, validate, convert, compare, and more.