C# Classes from JSON – Complete Guide
Learn how to generate C# classes from JSON data. Understand class properties, nullable types, List<T> collections, and .NET naming conventions for clean model classes.
What Are C# Model Classes?
In C# and .NET development, model classes (also called POCOs — Plain Old CLR Objects) define the structure of data you work with. They're essential for JSON deserialization, Entity Framework, API controllers, and data transfer objects (DTOs).
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
}
Why Generate Classes from JSON?
When consuming APIs or processing JSON files, you need C# classes that match the JSON structure. Generating them automatically:
- Eliminates manual mapping errors
- Saves significant development time for complex payloads
- Follows .NET conventions (PascalCase properties) automatically
Property Types
Primitive Mappings
| JSON Type | C# Type |
|---|---|
| string | string |
| integer | int |
| decimal number | double |
| boolean | bool |
| null | object (or nullable) |
Nullable Value Types
When a JSON value is null and the inferred type is a value type, C# uses nullable syntax:
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
public int? Discount { get; set; } // nullable int
public bool? InStock { get; set; } // nullable bool
}
Reference types like string are inherently nullable in C# (before nullable reference types), so they don't need the ? suffix.
Nested Objects
Each nested JSON object becomes a separate class:
{
"order": {
"id": 123,
"customer": {
"name": "Alice",
"email": "alice@example.com"
}
}
}
Produces:
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
public class Order
{
public int Id { get; set; }
public Customer Customer { get; set; }
}
public class RootObject
{
public Order Order { get; set; }
}
Collections (List<T>)
JSON arrays become List<T> in C#:
Simple Arrays
{ "scores": [95, 87, 92] }
→ public List<int> Scores { get; set; }
Arrays of Objects
{ "items": [{ "name": "Widget", "qty": 5 }] }
→ Creates an Item class and public List<Item> Items { get; set; }
Mixed Arrays
{ "data": [1, "text", true] }
→ public List<object> Data { get; set; } (falls back to object)
Naming Conventions
Our converter follows standard .NET naming conventions:
- Classes: PascalCase (e.g.,
ShippingAddress,OrderItem) - Properties: PascalCase (e.g.,
FirstName,ZipCode) - JSON keys like
first_nameare automatically converted toFirstName
Using with JSON Serialization
If you need to deserialize JSON with different key casing, use JsonPropertyName:
using System.Text.Json.Serialization;
public class User
{
[JsonPropertyName("first_name")]
public string FirstName { get; set; }
}
Best Practices
- Use meaningful root names —
ApiResponse,OrderPayload, etc. instead ofRootObject - Add System.Collections.Generic — Generated classes use
List<T>which requires this namespace - Consider nullable reference types — In C# 8+, enable nullable reference types for better null safety
- Add data annotations — Consider adding
[Required]or[MaxLength]attributes for validation - Use records for immutable data — In C# 9+, convert to
recordtypes if the data is read-only