JSON in C# — System.Text.Json & Newtonsoft
C# offers two main JSON libraries: System.Text.Json (built into .NET, high performance) and Newtonsoft.Json / Json.NET (widely used, more flexible). System.Text.Json is the recommended default for new .NET projects.
Deserialize JSON with System.Text.Json
Use JsonSerializer.Deserialize<T>() to parse JSON into a strongly-typed class.
using System.Text.Json;
public record User(int Id, string Name, string Email);
string json = """{"Id":1,"Name":"Alice","Email":"alice@example.com"}""";
User? user = JsonSerializer.Deserialize<User>(json);
Console.WriteLine(user?.Name); // AliceSerialize to JSON
Use JsonSerializer.Serialize() to convert an object to a JSON string.
var user = new User(1, "Alice", "alice@example.com");
string json = JsonSerializer.Serialize(user);
// {"Id":1,"Name":"Alice","Email":"alice@example.com"}
// Pretty-print
string pretty = JsonSerializer.Serialize(user, new JsonSerializerOptions { WriteIndented = true });Camel-case property names
Use JsonSerializerOptions to serialize property names in camelCase.
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
string json = JsonSerializer.Serialize(user, options);
// {"id":1,"name":"Alice","email":"alice@example.com"}Newtonsoft.Json (Json.NET)
If you need more flexibility (dynamic JSON, custom converters, LINQ to JSON), use Newtonsoft.Json.
using Newtonsoft.Json;
// Deserialize
User? user = JsonConvert.DeserializeObject<User>(json);
// Serialize
string output = JsonConvert.SerializeObject(user, Formatting.Indented);Related Tools
Frequently Asked Questions
How do I parse JSON in C#?▾
Use JsonSerializer.Deserialize<T>(jsonString) from System.Text.Json. Define a class or record matching the JSON shape. For Newtonsoft: JsonConvert.DeserializeObject<T>(json).
What is the difference between System.Text.Json and Newtonsoft.Json?▾
System.Text.Json is built into .NET 5+ and is faster and memory-efficient. Newtonsoft.Json has more features and flexibility. For new projects, System.Text.Json is recommended.
How do I handle camelCase JSON in C#?▾
Pass JsonSerializerOptions with PropertyNamingPolicy = JsonNamingPolicy.CamelCase. Or use [JsonPropertyName("camelCaseKey")] attribute on individual properties.
How do I deserialize JSON to a dynamic object in C#?▾
Use JsonSerializer.Deserialize<JsonElement>(json) and navigate with GetProperty() and GetString()/GetInt32(). Or use dynamic with Newtonsoft: JsonConvert.DeserializeObject<dynamic>(json).
How do I handle null values in C# JSON deserialization?▾
Mark properties as nullable (string?, int?) to accept null JSON values. Use JsonIgnoreCondition.WhenWritingNull in options to omit nulls during serialization.
JSON in Other Languages
Format and validate your JSON instantly
Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.
Open JSON Formatter →If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.