JSON to C# Class
Generate C# classes with System.Text.Json or Newtonsoft.Json attributes from any JSON object.
What is C# JSON Serialization?
In C#, JSON serialization converts between C# objects (classes, records, structs) and JSON strings. The .NET ecosystem has two main serializers: System.Text.Json (built-in since .NET Core 3.0, preferred for new projects) and Newtonsoft.Json (the classic library, still dominant in older codebases).
When consuming REST APIs in ASP.NET Core or calling external services, you typically define C# model classes that match the JSON structure. The serializer handles conversion automatically when you use JsonSerializer.Deserialize<T>() or HttpClient.GetFromJsonAsync<T>(). This tool generates the complete class hierarchy from your JSON instantly.
System.Text.Json vs Newtonsoft.Json
| Feature | System.Text.Json | Newtonsoft.Json |
|---|---|---|
| Attribute | [JsonPropertyName] | [JsonProperty] |
| Package | Built into .NET 3+ | Newtonsoft.Json NuGet |
| Performance | Faster (span-based) | Slower but more flexible |
| Null handling | Explicit nullable | More lenient |
| Best for | .NET 6+ / ASP.NET Core | Legacy / complex scenarios |
Example: System.Text.Json Output
csharp
using System.Text.Json.Serialization;
using System.Collections.Generic;
public class Address
{
[JsonPropertyName("street")]
public string Street { get; set; }
[JsonPropertyName("city")]
public string City { get; set; }
}
public class Root
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("address")]
public Address Address { get; set; }
}Deserialize with System.Text.Json
csharp
using System.Text.Json;
var root = JsonSerializer.Deserialize<Root>(jsonString);
// Or with options:
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var root2 = JsonSerializer.Deserialize<Root>(jsonString, options);Type Mapping
| JSON Type | C# Type | Notes |
|---|---|---|
| string | string | Nullable reference type |
| integer | long | Avoids int overflow |
| float | double | Double precision |
| boolean | bool | Non-nullable |
| null | object? | Nullable object |
| array | List<T> | System.Collections.Generic |
| object | class | New named C# class |