JSON to C# Class
The JSON to C# Class Generator converts a JSON sample into C# class definitions with auto-implemented properties and System.Text.Json or Newtonsoft.Json attributes. Use it to generate typed models for ASP.NET Core controllers, HttpClient response deserialisation, Azure Function payloads, and Blazor API data binding.
What Is the JSON to C# Class Generator?
The JSON to C# Class Generator analyses a JSON sample and produces C# class definitions with auto-implemented properties, ready for use with System.Text.Json (the built-in .NET JSON library since .NET Core 3.0) or Newtonsoft.Json (Json.NET). Each JSON object becomes a C# class with public auto-properties, and each JSON key becomes a property with the appropriate C# type. Optional [JsonPropertyName] attributes are added for System.Text.Json, or [JsonProperty] for Newtonsoft.Json, to preserve exact JSON key name mapping.
C# is a statically typed language that requires class or record definitions before you can work with structured data in a type-safe way. Without a typed class, developers must use dynamic or JsonElement for JSON access — both of which are verbose and lose compile-time safety. With a generated class, JsonSerializer.Deserialize<MyClass>(jsonString) produces a fully typed C# object in one line, giving you IntelliSense autocomplete, compile-time type checking, and refactoring support across the entire codebase.
According to the Microsoft documentation for System.Text.Json, the recommended approach for JSON serialisation in .NET is to define plain C# classes or records with public properties that match the JSON structure. This generator produces those classes from a real JSON sample, eliminating manual transcription of property names and types.
This generator is used by ASP.NET Core developers typing API request and response models, .NET developers integrating third-party REST APIs, Azure Function developers processing JSON event payloads, and Blazor developers defining data models for API calls.
How to Use the JSON to C# Class Generator
- Paste your JSON sample. The input should be a representative JSON object or array. Include null values and all possible fields to produce the most accurate class definition. Fields with null values generate nullable types (
string?in C# 8+ with nullable reference types enabled, orNullable<int>for value types). - Enter a class name. Enter a PascalCase class name — for example
User,Product, orApiResponse. Nested objects generate additional classes named from the property key in PascalCase. - Choose your JSON library. Select System.Text.Json (default, built-in to .NET) to add
[JsonPropertyName("keyName")]attributes. Select Newtonsoft.Json to add[JsonProperty("keyName")]attributes instead. - Review type mappings. JSON strings become
string. JSON integers becomeintorlong. JSON floats becomedouble. JSON booleans becomebool. JSON null values produce nullable types. JSON arrays becomeList<T>orT[]. - Copy and use. Paste the generated classes into your C# project. Add
using System.Text.Json;and deserialise:var user = JsonSerializer.Deserialize<User>(jsonString);
Why Use This Tool
A JSON object with 15 fields, 3 nested objects, and 2 list properties requires 15 property declarations, appropriate type annotations, and JSON attribute decorations — roughly 50 lines of C# class code when written with full property names and attributes. Writing this manually from an API sample is tedious. This tool generates the complete class structure in seconds from a real JSON sample.
ASP.NET Core REST API developers
ASP.NET Core uses System.Text.Json by default for deserialising request bodies and serialising response objects. A developer adding a new controller action that accepts a complex request body needs a C# class matching the expected JSON structure. This generator produces that class from a sample request payload, giving the developer a typed request model that the framework will validate and populate automatically via model binding.
.NET developers consuming third-party APIs
Any .NET service calling a third-party REST API — GitHub, Stripe, SendGrid, Twilio — needs C# classes matching the API's JSON response shapes. This generator produces those classes from a real API response captured in Postman or from the API documentation sample, eliminating the need to transcribe dozens of property names and types manually.
Azure Function developers processing event payloads
Azure Functions triggered by Service Bus, Event Grid, or HTTP endpoints receive JSON payloads as their input. Strongly typing these payloads with C# classes — deserialised with JsonSerializer.Deserialize — makes function code more readable, easier to test, and safer to refactor than working with raw string or JObject inputs. This generator produces the typed classes from a real event payload sample.
Blazor and MAUI developers calling APIs
Blazor WebAssembly and MAUI applications call REST APIs using HttpClient and deserialise JSON responses into C# objects. Developers define model classes in a shared project that is referenced by both the front-end and back-end. This generator produces those shared model classes from API response samples, ensuring front-end data binding uses the same strongly typed models as back-end business logic.
Real-World Use Cases
ASP.NET Core developer integrating a payment gateway
A back-end developer builds a payment processing service in ASP.NET Core that integrates with Stripe's API. Stripe returns complex nested JSON for payment intents, customers, and charge objects. The developer captures real Stripe test response payloads from the Stripe CLI, pastes each into this generator, and produces C# classes for PaymentIntent, Customer, PaymentMethod, and their nested types. The classes are placed in a Models/Stripe/ directory and used with JsonSerializer.Deserialize throughout the integration layer.
.NET developer building a GitHub repository analyser
A developer builds a .NET console tool that queries the GitHub REST API to generate contributor statistics. The GitHub API returns detailed JSON objects for repositories, commits, and contributors. The developer pastes a real API response into this generator and produces the C# class hierarchy — Repository, Owner, License, Topics — in seconds. The generated classes are used with HttpClient.GetFromJsonAsync<Repository>() in .NET 5+, which deserialises directly from the HTTP response stream.
Azure Function processing IoT device telemetry
A platform engineer implements an Azure Function triggered by IoT Hub messages from manufacturing floor sensors. Each message is a JSON payload containing device ID, timestamp, temperature, pressure, and alarm status. The engineer generates a SensorReading C# class from a sample message, uses it as the strongly typed input binding, and writes the function body knowing that all property accesses are compile-time checked. The function processes thousands of messages per minute, and the typed approach makes the code easier to maintain and test than string-based JSON parsing.
Blazor developer building a data dashboard
A Blazor WebAssembly developer builds a business intelligence dashboard that displays sales data from an internal REST API. The API returns paginated JSON responses with data arrays and metadata. The developer generates the C# response model classes from a real API response, places them in a shared class library referenced by both the Blazor app and the API project, and uses them to type both the HTTP calls in the Blazor app and the controller return types in the API — ensuring that any change to the response structure is caught immediately by the compiler across both projects.
Common Mistakes and Troubleshooting
Nullable reference types and null handling
C# 8.0 introduced nullable reference types (#nullable enable). With nullable reference types enabled, string is non-nullable by default — assigning or deserialising null to it produces a compiler warning. For JSON fields that can be null, use string?. For value types that can be null (int, bool, double), use int? (equivalent to Nullable<int>). The generator adds ? to types that were null in the sample. If your project does not use nullable reference types, the ? on reference types has no effect and can be left as-is or removed.
PascalCase property names not matching camelCase JSON keys
C# property naming convention is PascalCase (UserName), while JSON APIs typically use camelCase (userName) or snake_case (user_name). System.Text.Json by default uses case-sensitive matching and expects property names to match exactly. Add [JsonPropertyName("userName")] to each property, or configure the serialiser globally with options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase. The generator adds [JsonPropertyName] attributes for any property whose JSON key does not match the C# property name.
Deserialisation returning null instead of the object
If JsonSerializer.Deserialize<MyClass>(jsonString) returns null instead of an object, the JSON was the literal null value or empty. Check that the JSON string is non-null and non-empty before deserialising. The return type of Deserialize<T> is T? (nullable) — always check for null: var result = JsonSerializer.Deserialize<User>(json) ?? throw new InvalidOperationException("JSON was null");.
System.Text.Json not supporting all Newtonsoft.Json features
System.Text.Json is faster than Newtonsoft.Json but has a smaller feature set. Features available in Newtonsoft.Json but absent or limited in System.Text.Json include: reference loops handling, camelCase dictionary keys, some DateTimeOffset formats, and [JsonConstructor] behaviour differences. If you are migrating from Newtonsoft to System.Text.Json and encounter issues, consult the Microsoft migration guide.
Root-level JSON arrays deserialising incorrectly
To deserialise a JSON array into a C# collection, specify the collection type: var users = JsonSerializer.Deserialize<List<User>>(jsonString); or var users = JsonSerializer.Deserialize<User[]>(jsonString);. Do not try to deserialise an array-rooted JSON into a non-collection class — Deserialize<User> expects a JSON object, not a JSON array, and will throw a JsonException if given an array.
Frequently Asked Questions
What C# types are generated for each JSON value type?
What is the difference between System.Text.Json and Newtonsoft.Json?
How do I deserialise JSON into the generated C# class?
What is the [JsonPropertyName] attribute and when do I need it?
Can I generate C# records instead of classes?
How do I handle nullable reference types in the generated code?
How are nested JSON objects handled in C# classes?
How do I configure System.Text.Json for camelCase property names globally?
How do I use the generated classes with HttpClient?
What happens when a JSON field has a value type that changes?
Rate This Tool
Was this tool helpful?
Be the first to rate this tool
About the Author
S. Siddiqui is the founder and editor-in-chief of YourToolsBase, overseeing all content, tool accuracy, and editorial standards.
View full profileAuthoritative Sources
Formulas and data in this tool are based on guidelines from the above sources.