JSON to C# Class Generator
Convert any JSON object into strongly-typed C# classes instantly. Generates proper C# properties with correct types, JsonProperty attributes for Newtonsoft.Json, JsonPropertyName for System.Text.Json, and supports nested objects, arrays, nullable types, and C# 9+ record types.
Convert any JSON object into strongly-typed C# classes instantly. Generates proper C# properties with correct types, JsonProperty attributes for Newtonsoft.Json, JsonPropertyName for System.Text.Json, and supports nested objects, arrays, nullable types, and C# 9+ record types.
This tool is designed to provide a seamless experience for developers by handling complex operations directly in your browser with maximum speed and security.
In .NET development, consuming a REST API or reading a JSON file always involves the same foundational step: you need a C# class that matches the JSON structure so a serializer can deserialize the JSON into typed objects your code can work with safely. JSON to C# conversion is the process of generating those C# class definitions automatically from a JSON sample, rather than writing them by hand property by property.
C# is a statically typed language — unlike JavaScript or Python, you cannot just receive JSON and access its properties dynamically without either defining the shape ahead of time or using dynamic/ExpandoObject, which sacrifices compile-time safety and IDE support. A properly typed C# class gives you IntelliSense autocomplete for every property, compile-time errors when you mistype a property name, and proper null reference handling with nullable types. The class is also what tells the serializer how to map JSON field names to C# property names, how to handle missing fields, and how to convert JSON types like strings that represent dates into proper C# types like DateTime.
.NET has two major JSON serialization libraries. Newtonsoft.Json (also called Json.NET) has been the dominant library for a decade and is still widely used — it uses the [JsonProperty] attribute for name mapping. System.Text.Json is the newer library built into .NET Core 3.0 and later — it is faster, allocates less memory, and is now the recommended library for new .NET projects, using [JsonPropertyName] for name mapping. This tool generates classes compatible with both, so you can pick the one your project uses.
This tool takes a JSON object and generates the C# class definitions that match its structure. It infers the correct C# type for each JSON field: JSON strings become string, JSON integers become int or long depending on size, JSON decimals become double or decimal, JSON booleans become bool, JSON arrays become List with the correct element type inferred, and JSON nested objects become separate named C# classes with their own properties. All inferred types are the idiomatic C# types that Newtonsoft.Json and System.Text.Json expect by default. For Newtonsoft.Json, the generated classes include [JsonProperty("fieldName")] attributes on each property so the serializer correctly maps JSON field names like user_id or userId to their C# property names like UserId following C# PascalCase conventions. For System.Text.Json, the equivalent [JsonPropertyName("fieldName")] attributes are generated instead. You get a complete, copy-pasteable C# class file with using directives, proper namespacing structure, and all nested types defined as separate classes. The tool also supports C# 9.0+ record types — immutable data structures that are increasingly preferred for API response models because they are thread-safe, have value-based equality, and work well with pattern matching. For nullable reference types, introduced in C# 8.0 and now the default in .NET 6+, the tool marks fields that appear optional or that have null values in the JSON sample as nullable types (string? instead of string, int? instead of int). This prevents null reference exceptions that are the most common runtime error when deserializing JSON with missing or null fields.
1. Paste your JSON into the Input JSON field — use a representative sample of the API response you are working with. A single JSON object works best for generating a class definition. If your API returns an array, paste just one element from the array since the tool generates the class for a single item. Make sure the JSON is valid before pasting — use the JSON Formatter and Validator tool if you are not sure.
2. Click Convert to C# Class — the tool parses every field in your JSON, infers the correct C# type for each field, generates separate classes for nested objects, creates List properties for arrays, adds [JsonProperty] or [JsonPropertyName] attributes to map JSON field names to PascalCase C# property names, and outputs the complete class definitions in the result panel.
3. Review the generated C# code — check that the inferred types look correct for your use case. JSON integers are inferred as int by default but your API might return values that require long for large IDs. JSON strings that represent dates should have their type changed to DateTime or DateTimeOffset. Fields that might be null in real responses should be marked as nullable types (string? int? bool?) if they are not already.
4. Copy the generated C# code using the Copy button and paste it into your .NET project — create a new file in your Models folder, or paste directly into an existing file. Add the appropriate using directive at the top: using Newtonsoft.Json; for Json.NET attributes or using System.Text.Json.Serialization; for System.Text.Json attributes.
5. Deserialize JSON into your new class using your serializer — with System.Text.Json: var obj = JsonSerializer.Deserialize(jsonString); and with Newtonsoft.Json: var obj = JsonConvert.DeserializeObject(jsonString); The generated attributes ensure field names map correctly without additional serializer configuration.
Writing C# model classes for a JSON API response by hand is one of those tasks that looks simple and is actually tedious. A JSON response with 15 fields, three nested objects, and two arrays of objects means writing 4 or 5 class definitions with 30 or 40 properties total, getting the attribute syntax right for each property, handling the nullable annotations, and making sure the property names follow C# conventions while correctly mapping to the JSON field names. For a single endpoint that takes 20 minutes — and any real API integration involves 10 to 20 endpoints minimum. The scenario I encounter most often is integrating a third-party API that has no official .NET SDK. You have the API documentation (or just a sample response from Postman), and you need typed models to work with the response in your ASP.NET Core application. Paste the sample response into this tool, get the C# classes, drop them into a Models folder in your project, and you are deserializing responses in minutes instead of hours. The generated attributes ensure that JsonSerializer.Deserialize() or JsonConvert.DeserializeObject() will correctly map every field without needing additional configuration. Record types deserve a specific mention. Since C# 9.0, records have been the preferred way to define API response models in modern .NET projects — they are immutable by default which means you cannot accidentally mutate a deserialized response object, they have built-in value equality which makes testing easier, and they generate clean ToString() output for logging. If you are starting a new .NET 6 or later project, choose the record type output option and your models will follow current best practices from day one.
Correct C# type inference — JSON strings become string
integers become int or long
decimals become double
booleans become bool
arrays become List with the correct element type
and nested objects become separate named C# classes
Supports both major .NET serializers — generates [JsonProperty] attributes for Newtonsoft.Json and [JsonPropertyName] attributes for System.Text.Json so you can pick the library your project uses
Record type support — generates C# 9.0+ record types for immutable API response models which are the current best practice for new .NET 6 and later projects
Nullable type support — marks fields that appear optional or null in the JSON sample as nullable reference types (string? int? bool?) to prevent null reference exceptions at runtime
PascalCase property names — generates idiomatic C# PascalCase property names (UserId not user_id) while keeping the correct JSON attribute for serialization mapping
Nested object handling — generates separate class definitions for every nested JSON object with proper $ref-style type references between the generated classes
100% browser-based — your JSON data is never sent to any server making it safe to use with API responses that contain internal field structures or sensitive data
Instant generation — all type inference and class generation happens in your browser in under a second with no server round-trip
Generating C# model classes for third-party REST API responses with no official .NET SDK
Creating strongly-typed request and response models for ASP.NET Core Web API endpoints
Converting JSON configuration file structures into typed C# settings classes for IOptions pattern
Generating C# classes for JSON payloads received from Azure Service Bus or AWS SQS message queues
Creating typed models for JSON data returned from CosmosDB or MongoDB document queries
Generating C# DTOs (Data Transfer Objects) from API response samples during integration development
Converting webhook payload JSON into typed C# classes for event handling
Bootstrapping model classes for Blazor applications consuming REST API endpoints
Example Input
{
"id": 1,
"name": "Priya Singh",
"email": "priya@learnhubly.com",
"isActive": true,
"tags": ["developer", "admin"],
"profile": {
"bio": "Principal Software Engineer",
"skills": ["Go", "React", "TypeScript"]
}
}Example Output
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class User
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("isActive")]
public bool IsActive { get; set; }
[JsonPropertyName("tags")]
public List Tags { get; set; }
[JsonPropertyName("profile")]
public Profile Profile { get; set; }
}
public class Profile
{
[JsonPropertyName("bio")]
public string Bio { get; set; }
[JsonPropertyName("skills")]
public List Skills { get; set; }
}Namespace Missing: The generated class does not include a namespace declaration because namespace depends on your project structure. Wrap the generated code in your project's namespace: namespace YourProject.Models { ... } or use a file-scoped namespace declaration in C# 10+: namespace YourProject.Models; — place this at the top of the file before the class definition.
Missing Using Directives: If your project shows errors after pasting the generated code, check that the required using directives are present at the top of the file. For System.Text.Json attributes add using System.Text.Json.Serialization; and for List properties add using System.Collections.Generic; — though in .NET 6+ with implicit usings enabled these are included automatically.
Integer Fields That Should Be Long: JSON integers are inferred as int (32-bit) by default. If your API returns large numeric IDs — Twitter IDs, Unix timestamps in milliseconds, or any integer above 2,147,483,647 — the field will overflow an int at runtime. Change the type from int to long for any ID fields or large numeric values before using the generated class.
String Fields That Should Be DateTime: JSON does not have a native date type — dates in JSON are always strings like 2026-05-20T14:30:00Z. The tool infers these as string because it cannot know from the value alone that the string represents a date. For any field you know represents a date or timestamp, change the type from string to DateTime, DateTimeOffset, or DateOnly depending on whether you need time and timezone information.
Nullable Reference Type Warnings in .NET 6+ Projects: In .NET 6 and later, nullable reference types are enabled by default. The generated classes may show warnings for non-nullable string and object properties that are not initialized in the constructor — this is because the serializer sets them during deserialization but the compiler does not know that. Fix by adding = null!; as the default value (public string Name { get; set; } = null!;) or by enabling the required modifier in C# 11 with public required string Name { get; set; }.
Using the generated class without checking for null reference exceptions on nested objects
Fix: If a nested object field can be null in real API responses — for example a profile object that is null for users who have not completed their profile — accessing properties on it without a null check will throw a NullReferenceException at runtime. The generated class has a Profile property of type Profile, but if the API returns null for that field, the property will be null after deserialization. Either mark the property as nullable (Profile? Profile) or add null checks before accessing nested properties: user.Profile?.Bio ?? string.Empty.
Generating the class from a minimal JSON sample that does not represent the full API response shape
Fix: The tool infers types and fields only from the JSON you paste. If your API response sometimes includes fields that are absent in your sample, those fields will not appear in the generated class and will be silently ignored during deserialization. Use the most complete JSON sample you can get — ideally from an API call that returns a fully populated response with all optional fields present. Check the API documentation for any fields not present in your sample and add them manually to the generated class.
Mixing Newtonsoft.Json and System.Text.Json attributes in the same project
Fix: If you generate classes with [JsonProperty] attributes (Newtonsoft.Json) but your project uses System.Text.Json as the serializer, the attributes will be silently ignored — System.Text.Json does not read [JsonProperty] attributes. Deserialization will still work for fields where the JSON name matches the C# property name, but it will fail for fields where they differ (like snake_case JSON names mapped to PascalCase properties). Decide which serializer your project uses — check your startup code or NuGet packages — and generate with the matching attribute style.
Not adding the class to the correct namespace and ending up with duplicate class names
Fix: The generated code has no namespace, so if you paste multiple generated classes from different API responses into the same project without namespacing them, you will get duplicate class name conflicts — two classes both called User or Profile from different APIs. Always wrap generated classes in a specific namespace that indicates their purpose: namespace YourApp.Models.Github { ... } and namespace YourApp.Models.Stripe { ... } for classes generated from different third-party APIs. This also makes it clear in the codebase where each model comes from.
Using mutable classes with public setters for API responses when records would be better
Fix: The default generated output uses { get; set; } properties which are mutable — any code in your application can change the values on a deserialized response object after deserialization, which can introduce subtle bugs where a response object is accidentally modified during processing. For API response models that should be read-only after deserialization, choose the record type output option or change { get; set; } to { get; init; } on each property. init-only properties can be set by the serializer during deserialization but cannot be modified afterward, giving you immutability without switching to records.
JSON Cheatsheet
Interactive reference guide for valid JSON syntax, data types, parsing/stringifying methods, schema validation rules, and language integrations.
Regex Cheatsheet
Interactive guide to Regex anchors, character classes, quantifiers, lookarounds, capturing groups, and search flags.
HTTP Headers Cheatsheet
Complete guide to standard and security HTTP headers including Authorization, CORS control, caching policies, and CSP directives.
SQL Cheatsheet
Complete guide to SQL statements including SELECT queries, WHERE filters, aggregate functions, JOIN types, and DDL commands.
Does it support Record types?
Yes. C# 9.0+ record types are supported as an output option. Records are immutable by default, have value-based equality, and are now the recommended model type for API responses in modern .NET 6 and later projects. A generated record looks like public record User(int Id, string Name, string Email) with init-only properties set during deserialization. Records work with both Newtonsoft.Json and System.Text.Json with the correct serializer configuration. Choose the record type output if you are starting a new .NET 6+ project.
Does it support Newtonsoft.Json and System.Text.Json?
Yes, both are supported. For Newtonsoft.Json (Json.NET), the generated class uses [JsonProperty("fieldName")] attributes on each property. For System.Text.Json, it uses [JsonPropertyName("fieldName")] attributes instead. These are mutually exclusive — the attributes from one library are not read by the other. Check your project's NuGet packages: if you have Newtonsoft.Json installed use those attributes, if you are on .NET Core 3.0 or later without Newtonsoft, use System.Text.Json attributes which are built into the framework.
What C# types are inferred from JSON?
JSON strings become string. JSON integers become int for typical values or long for very large values. JSON decimal numbers become double. JSON booleans become bool. JSON null values make the property nullable. JSON arrays become List with the element type inferred from the array contents. JSON nested objects become separate named C# classes referenced by the parent class. Date strings like 2026-05-20T14:30:00Z are inferred as string since JSON has no date type — change these to DateTime or DateTimeOffset manually after generating.
Can I use this with ASP.NET Core Web API?
Yes. ASP.NET Core Web API uses System.Text.Json by default since .NET Core 3.0. Generate your classes with System.Text.Json attribute style, add them to your Models folder, and use them as action method return types or as deserialization targets for HttpClient responses. For model binding (deserializing request bodies), ASP.NET Core automatically deserializes the request body into the typed model class using the [JsonPropertyName] attributes for field mapping. For consuming external APIs, use HttpClient with JsonSerializer.DeserializeAsync() using the generated class.
How do I handle JSON fields with snake_case names in C#?
C# convention is PascalCase for public properties (UserId not user_id). The generated class handles this automatically — it generates a PascalCase property name and adds the original JSON field name as the attribute value: [JsonPropertyName("user_id")] public int UserId { get; set; }. During deserialization, the serializer reads the user_id field from JSON and maps it to the UserId C# property using the attribute. You never need to manually handle the case conversion — that is exactly what the JsonProperty and JsonPropertyName attributes are for.
Does it handle nullable reference types for .NET 6+?
Yes. For fields that appear as null or are absent in your JSON sample, the tool generates nullable types — string? instead of string, int? instead of int, CustomClass? instead of CustomClass. In .NET 6 and later where nullable reference types are enabled by default, this prevents null reference warnings at compile time and null reference exceptions at runtime. For fields that are never null in the API response, the tool generates non-nullable types. Review the generated output and mark any additional fields as nullable if you know they can be null in real API responses.
What should I do if the generated class has too many nested classes?
For deeply nested JSON with many levels of nested objects, the tool generates a separate class for each nested object. This is correct behavior but can result in many small class files. You have two options: put all generated classes in a single file for simple cases — C# allows multiple class definitions in one file — or split them into separate files in a Models folder following the one-class-per-file convention. For very deeply nested structures, consider whether the API response design itself could be simplified. If you control the API, flatter response objects are generally easier to consume on the client side.
How do I deserialize a JSON array into a List of the generated class?
If the API returns a JSON array like [{...}, {...}, {...}] rather than a single object, deserialize into List rather than YourClass directly. With System.Text.Json: var list = JsonSerializer.Deserialize>(jsonString); and with Newtonsoft.Json: var list = JsonConvert.DeserializeObject>(jsonString); The generated class describes a single item — the List wrapper handles the array. If the API returns a paginated response wrapper like {data: [...], total: 100}, create a separate wrapper class like public class PagedResponse { public List Data { get; set; } public int Total { get; set; } } and deserialize as PagedResponse.
How to Convert JSON to Dart Classes for Flutter – Complete In-Depth Guide (2026)
How to convert JSON to Dart classes for Flutter in 2026. In-depth guide covering Dart model best practices, null safety, immutable classes, code generation with json_serializable & freezed, Flutter integration with Riverpod, and real-world architecture tips from a Principal Software Engineer with 15+ years experience.
What is JSON? How to Format, Validate & Use It (Complete Guide 2026)
What is JSON? How to Format, Validate & Use It (Complete Guide 2026). In-depth explanation of JSON syntax, real-world use cases, formatting best practices, common mistakes, advantages, disadvantages, and expert tips from a Principal Software Engineer with 15+ years experience.
How to validate JSON online (step-by-step guide)
Invalid JSON can break your application. Follow this guide to quickly validate and fix your JSON data.
Recent Activity
No recent activity