JSON to C# Classes
Paste a JSON sample to generate matching C# class definitions. Nested
objects become nested classes and arrays become List<T>.
Everything runs in your browser, so you can paste real API responses safely.
Why generate classes at all
Consuming a JSON API in C# means deserializing into something. You can use
dynamic or JsonDocument and index by string, but
then every field access is unchecked: a typo compiles fine and fails at
runtime, and the compiler cannot help with refactoring or IntelliSense.
Typed classes turn the API contract into something the compiler understands. Writing the classes by hand for a large response is tedious and error-prone, which is what this tool removes.
Type inference rules
C# types are inferred from the JSON values in the sample:
| JSON value | Generated C# type |
|---|---|
"text" | string |
42 | int |
9999999999 | long (outside int32 range) |
3.14 | double |
true | bool |
null | object, with a comment |
{...} | A nested class |
[...] | List<T> |
Property names are converted to PascalCase, so user_name and
userName both become UserName, matching .NET naming
conventions.
The limits of inferring from one sample
This is worth understanding, because it determines how much you need to review the output.
A single JSON example shows what the data looked like once. It cannot show what the API is capable of returning. Specific consequences:
- Nullability is invisible. If a field happens to have a value in your sample, nothing indicates it can be null in other responses. Generated properties are non-nullable, which will throw at runtime when null does arrive.
- Empty arrays give no element type.
"items": []contains no information about what the items would be. - Numeric widening. A field showing
1becomesint, but if the API can return1.5, it should have beendouble. - Optional fields are absent. Fields missing from your sample simply do not appear in the generated class.
Treat the output as a solid first draft that saves the typing, then check it against the actual API documentation.
Examples
| JSON | Generated C# |
|---|---|
{"id": 1, "name": "Alice"} |
public class Root
{
public int Id { get; set; }
public string Name { get; set; }
} |
{"user": {"city": "Berlin"}} |
public class User
{
public string City { get; set; }
}
public class Root
{
public User User { get; set; }
} |
Adjusting the generated code
Property name mapping
If the JSON uses snake_case, the PascalCase properties will not match during deserialization unless you configure it. Two options with System.Text.Json: set a naming policy globally, or annotate individual properties:
[JsonPropertyName("user_name")]
public string UserName { get; set; }
The global approach is usually cleaner:
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
Nullable reference types
In projects with nullable reference types enabled, add ? to
any property the API may omit or return as null:
public string? MiddleName { get; set; }. This makes the compiler
enforce null checks at the point of use.
Dates
JSON has no date type, so ISO 8601 date strings are inferred as
string. Change these to DateTime or
DateTimeOffset manually, System.Text.Json parses ISO 8601 into
both automatically. Prefer DateTimeOffset when the value carries
a time zone offset.
Collection class names
Array element classes take their name from the property, so
"items" produces a class named Items rather than
Item. Rename it if the singular reads better, the tool does not
attempt singularization because English pluralization is too irregular to do
reliably.
Frequently asked questions
What happens with a null value?
The property is typed object with a comment noting that the
type could not be inferred. Check the API documentation and replace it with
the real type.
Are the generated classes ready to use with System.Text.Json?
Yes for straightforward payloads. If the JSON uses snake_case or
camelCase keys, configure a naming policy or add
[JsonPropertyName] attributes so the names line up.
What about array element class names?
They derive from the property name, for example items
produces class Items. Rename manually if you prefer the
singular.
Does this work from only one JSON sample?
Yes, and that is its main limitation. Types are inferred from a single example, so optional fields, nullability, and alternate types are not detected. Review the result against the API contract.
Should properties be nullable?
Any field the API may omit or return as null should be. The tool cannot
determine this from one sample, so add ? where the documentation
indicates optionality.
Is my JSON sent anywhere?
No. Parsing and code generation run entirely in your browser, so pasting a real API response containing production data is safe.
Related tools
- JSON formatter, to tidy a payload before generating classes
- Case converter, for individual identifier renaming
- CSV and JSON converter, for tabular sources
- All developer tools