JSON to C# Class Generator
Infer C# records or classes with nested models, nullable types, and JSON serializer attributes
Generated C# models will appear here.
About this tool
JSON to C# Class Generator infers reusable models from a JSON object or a non-empty array of object samples. It merges multiple samples to identify optional properties, nested objects, lists, integer and floating-point values, booleans, strings, and unknown or null-only fields. Output can use positional records or mutable classes, file-scoped namespaces, nullable reference types, and exact property mappings for either System.Text.Json or Newtonsoft.Json. JSON names that do not exactly match the emitted C# identifier receive an explicit serializer attribute, so snake_case and unusual names remain mapped without relying on a global naming policy. Inference is a starting point rather than an API contract: dates, decimals, enums, dictionaries, custom converters, validation attributes, required members, and polymorphic payloads still require developer review.
Scenario Recipes
Bootstrap DTOs from two API response samples
Goal: Expose optional response fields before integrating a C# client
- Capture two sanitized response objects that cover both common and optional fields, then place them in one JSON array.
- Generate classes with nullable references and the serializer used by the target project.
- Review null-only values and domain strings, replacing object, string, long, or double with approved DTO types before committing.
Result: A reviewable DTO baseline whose optional fields and exact JSON property mappings are explicit.
Frequently Asked Questions
Can it generate both records and classes?
Yes. Choose positional records for compact immutable models or mutable classes with get and set accessors.
How are optional fields detected?
When an array contains multiple object samples, a property missing from any sample is inferred as optional and may receive a nullable type.
Why are JsonPropertyName attributes added?
They preserve exact JSON keys whenever a generated C# property name differs, including snake_case, punctuation, and reserved words.
Does it support System.Text.Json and Newtonsoft.Json?
Yes. Select either attribute family, or omit serializer attributes when your project supplies its own naming policy.
Will it infer DateTime, decimal, or enums?
No. Strings remain strings and general numbers use long or double; domain-specific types require manual review.
Is the JSON sent to a server?
No. Parsing, model inference, code generation, copying, and downloads happen locally.
Keep browsing