jsondecode.com logo

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); // Alice

Serialize 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);

Format and validate your JSON instantly

Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.

Open JSON Formatter →