PureDevTools

JSON to C# Class Generator

Paste JSON, get C# classes or records — nested objects, List types, nullable types, [JsonPropertyName] — copy or download as .cs

All processing happens in your browser. No data is sent to any server.

Options

361 characters

3 classes generated · 823 characters

Classes generated: 3Output size: 823 charsOptions: classes

You’re consuming a REST API in C# and need a model class that matches the JSON response. The response has 20 fields, nested address and metadata objects, and an array of line items. JsonSerializer.Deserialize<MyClass>(json) is one line of code — but writing MyClass with all its nested types by hand takes 15 minutes and leaves room for typos that cause silent deserialization failures. This tool generates the complete C# model in seconds.

Why Generate C# Models from JSON?

Deserialization with System.Text.Json: The standard library deserializer maps JSON properties to C# properties by name. If you have a JSON key "firstName" and a C# property LastName, that field is silently ignored. Getting the property names right from a real JSON payload eliminates this class of bug at the start.

Newtonsoft.Json (Json.NET): The most widely used JSON library in .NET works with the same POCO (Plain Old CLR Object) classes this tool generates. Both JsonSerializer (System.Text.Json) and JsonConvert (Newtonsoft) use the public properties with { get; set; }.

Speed: Generating from a real API response catches every field, including optional or rarely populated ones. Manual class writing tends to miss fields that only appear under certain conditions.

Classes vs Records

This tool supports both C# classes (the traditional approach) and records (introduced in C# 9).

Classes

Standard class output:

public class Root
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Address Address { get; set; }
}

Classes are mutable by default and work with all JSON libraries. They support post-construction property assignment, which is required by System.Text.Json’s default deserializer and Newtonsoft.Json.

Records (C# 9+)

Record output uses positional parameters:

public record Root(
    string Name,
    int Age,
    Address Address
);

Records are immutable by default, provide value equality, and generate ToString() automatically. They work with System.Text.Json in .NET 6+ with no extra configuration. Records are ideal for API response models where you don’t need to mutate the data after deserialization.

System.Text.Json Attributes

When [JsonPropertyName] is enabled, each property gets an attribute that explicitly maps it to the JSON key:

public class Root
{
    [JsonPropertyName("firstName")]
    public string FirstName { get; set; }

    [JsonPropertyName("is_active")]
    public bool IsActive { get; set; }
}

This is necessary when the JSON uses camelCase or snake_case keys and your C# properties use PascalCase. Without this attribute, System.Text.Json uses case-insensitive matching by default, which works in most cases but is more brittle. Using explicit attributes is the recommended approach for production code.

To use [JsonPropertyName], add using System.Text.Json.Serialization; — this import is generated automatically by the tool when the option is enabled.

Nullable Reference Types

When nullable reference types are enabled, the tool emits #nullable enable at the top of the file and marks value types with ? when their JSON value is null:

#nullable enable

public class Root
{
    public string Name { get; set; }
    public int? Age { get; set; }        // null in some responses
    public object? DeletedAt { get; set; }
}

Nullable reference types (NRT) were introduced in C# 8. With NRT enabled, the compiler warns you when you try to use a potentially null reference without checking it first. This catches null reference exceptions at compile time rather than at runtime.

Handling Nested Objects

When a JSON value is itself an object, the generator creates a separate class named after the parent class and the property key in PascalCase:

{ "profile": { "bio": "Engineer", "avatar": "https://example.com/pic.jpg" } }

This generates Root (with a Profile property of type RootProfile) and a separate RootProfile class. Each nested class is included in the output within the same namespace block.

Arrays and List Types

JSON arrays map to List<T>. The using System.Collections.Generic; import is added automatically. When an array contains objects, the generator merges all array elements into a single class:

{ "tags": ["developer", "dotnet"], "posts": [{ "id": 1 }, { "id": 2, "draft": true }] }

Deserializing in Practice

System.Text.Json:

using System.Text.Json;

var root = JsonSerializer.Deserialize<Root>(jsonString);

Newtonsoft.Json:

using Newtonsoft.Json;

var root = JsonConvert.DeserializeObject<Root>(jsonString);

HttpClient integration:

var root = await httpClient.GetFromJsonAsync<Root>("/api/users/1");

FAQ

Does this tool send my JSON to a server?

No. All processing runs entirely in your browser using JavaScript. Your JSON data never leaves your device.

Which .NET version supports records?

Records were introduced in C# 9 / .NET 5. File-scoped namespace declarations (used in the record output) require C# 10 / .NET 6. If you’re on an older version, switch the toggle to classes.

What if my JSON root is an array?

The tool generates an item class (e.g., RootItem) and adds a comment showing how to deserialize the array: JsonSerializer.Deserialize<List<RootItem>>(json).

How does this differ from Newtonsoft vs System.Text.Json?

The generated classes work with both. The [JsonPropertyName] attribute is specific to System.Text.Json. For Newtonsoft, use [JsonProperty("key")] from Newtonsoft.Json instead. The classes themselves are identical.

What about null JSON values?

JSON null maps to object (or object? with nullable reference types enabled), since the actual C# type is unknown from a null value alone. Refine these fields to the appropriate type after copying the generated code.

Can I use the records output with ASP.NET Core?

Yes. ASP.NET Core 6+ uses System.Text.Json by default and fully supports record deserialization. Records with positional parameters are deserialized via their constructor, which System.Text.Json handles correctly.

Related Tools

More JSON Tools