JSON to C# Class
Generate C# record / class definitions from a JSON sample.
Overview
The JSON-to-C# generator produces strongly typed C# class or record definitions from a sample JSON document. Property names are PascalCased, types are inferred from the values, and nullable annotations are added based on whether keys are missing or values are null in the source.
It's the fastest way to scaffold DTOs for a third-party API response, mirror a webhook payload as a typed model, or generate fixtures for a System.Text.Json deserialiser test. .NET developers reach for a json to c# converter to skip the busywork of typing out property declarations by hand.
How it works
The generator walks the JSON tree, names the root class from a configurable name (default Root), and recursively creates nested types for each object encountered. Property types are inferred: strings stay as string, integers become int or long based on magnitude, decimals become decimal or double, and arrays become List<T> of the element type. Heterogeneous arrays fall back to List<object>.
Output style is configurable: class with auto-properties, record with positional parameters, or record with init-only properties. JsonPropertyName attributes are added so the original JSON keys survive PascalCasing.
Examples
Sample:
{ "id": 1, "name": "Alice", "tags": ["admin","ops"], "address": { "city": "Berlin" } }
public record Root(
int Id,
string Name,
List<string> Tags,
Address Address
);
public record Address(string City);
// With class style and System.Text.Json attributes:
public class Root {
[JsonPropertyName("id")] public int Id { get; set; }
[JsonPropertyName("name")] public string Name { get; set; } = string.Empty;
...
}
FAQ
Does it use System.Text.Json or Newtonsoft attributes?
Both options are available. System.Text.Json uses [JsonPropertyName]; Newtonsoft.Json uses [JsonProperty]. Pick whichever your project already depends on.
Are nullable reference types supported?
Yes. Properties that can be null (because the sample has null or omits the key in some objects) are typed with the ? suffix. Make sure your project has <Nullable>enable</Nullable> for the annotations to take effect.
Can it produce immutable records vs. mutable classes?
Both styles are supported via a single switch. Records are recommended for DTOs you don't intend to mutate after deserialisation; classes are more flexible for older code bases without C# 9 record support.