CMD Simulator

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 TypeC# Type
stringstring
integerint
decimal numberdouble
booleanbool
nullobject (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_name are automatically converted to FirstName

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

  1. Use meaningful root namesApiResponse, OrderPayload, etc. instead of RootObject
  2. Add System.Collections.Generic — Generated classes use List<T> which requires this namespace
  3. Consider nullable reference types — In C# 8+, enable nullable reference types for better null safety
  4. Add data annotations — Consider adding [Required] or [MaxLength] attributes for validation
  5. Use records for immutable data — In C# 9+, convert to record types if the data is read-only

Ready to try it?

Open the Converter →