JSON to C# Class Generator
Infer C# records or classes with nested models, nullable types, and JSON serializer attributes
Inferred from samples, not a complete API contract. Missing fields and explicit null are inferred separately but can share an output type; null array elements remain nullable. Duplicate keys and numbers that would change value are rejected.
Limits: 1 MiB, 64 levels, 20,000 values, 200 models and 2,000 model fields. Use strings for exact large IDs or monetary decimals.
Target: C# 10+. System.Text.Json ships with .NET; Newtonsoft.Json needs a project dependency. Record parameters have no defaults, avoiding optional parameters before required ones.
After generation, compile in your target project and verify deserialization with representative responses.
About this tool
JSON to C# Class and Record generates C# 10+ source with file-scoped namespaces, nested models and System.Text.Json or Newtonsoft.Json mappings. Source field names are preserved through serializer attributes while generated member names remain unique even under case-insensitive constructor matching. Newtonsoft record parameters receive both property and parameter mappings so similarly normalized names cannot steal each other's values. Records omit parameter defaults, avoiding an optional parameter before a required one; mutable classes use get/set properties. Null array elements become nullable element types such as List<long?>. Missing and explicit null fields are observed separately but the generated nullable model does not retain presence history. Unknown values use object, and disabling nullable reference annotations does not prevent runtime nulls. Numeric inference uses long or double only after rejecting unsafe integers or original tokens that would change value; exact amounts and large identifiers should be strings. All work is local within 1 MiB, 64 levels, 20,000 values, 200 models and 2,000 fields. Compile in the target .NET project and deserialize representative responses before adding decimal, DateTime, enum, required-member or custom-converter contracts.
Scenario Recipes
Verify similarly named JSON keys in a Newtonsoft record
Goal: A checked constructor mapping without value swaps between normalized names.
- Paste {"a-b":1,"a_b":2,"aB2":3} and select Record with Newtonsoft.Json.
- Inspect unique member names and both property and parameter JsonProperty attributes.
- Deserialize with Newtonsoft.Json, serialize again and verify that each of the three original keys retains its distinct value.
Result: A checked constructor mapping without value swaps between normalized names.
Failure Clinic (Common Pitfalls)
Two JSON keys populate the wrong record parameter
Cause: Normalized names can collide under a serializer's constructor matching rules.
Fix: Keep unique generated names and both Newtonsoft attribute targets, then verify each original key with distinct sample values.
A large ID changes before it reaches long
Cause: Browser number parsing can round a JSON token even though C# long could store the original integer.
Fix: Keep the ID as a quoted string for generation and use an explicit reviewed conversion in the target project.
Production Snippets
Nullable list elements in a System.Text.Json record
csharp
// Input: {"values":[1,null,2]}
public record ApiResponse(
[property: JsonPropertyName("values")] List<long?> Values
);
// Requires System.Collections.Generic and System.Text.Json.Serialization.Frequently Asked Questions
Why are record parameters not assigned null defaults?
An optional parameter before a required parameter is illegal in C#. The generator keeps positional parameters without defaults; sample nullability is expressed in their types.
Why does Newtonsoft output have property and parameter attributes?
Constructor parameter matching can confuse similarly normalized names. Both targets receive the original JSON name so deserialization and serialization use the same mapping.
Does nullable reference mode change JSON runtime validation?
No. It changes compiler annotations. Runtime values can still be null, and required fields or converters must be configured separately.
Can I infer decimal or DateTime automatically?
No. JSON samples do not identify money, time zones or domain constraints. Use strings for exact source values and choose tested domain conversions in the application.
Keep browsing