JSON to C# Class Generator - Classes and Records
DTOs for System.Text.Json
Paste a response body and get the DTOs to deserialize it with JsonSerializer.Deserialize<T>.
Properties are PascalCase, and each one has a [JsonPropertyName] with the exact JSON key. That
makes binding independent of JsonSerializerOptions: you don't need
PropertyNameCaseInsensitive or a naming policy for it to work.
Example: a product with variants
Input, with the root type name set to Product:
{
"sku": "WIDGET-9",
"price": 19.99,
"stock": 140,
"tags": ["hardware", "sale"],
"dimensions": { "width": 12.5, "height": 4 },
"variants": [
{ "color": "red", "stock": 20 },
{ "color": "blue", "stock": 0, "discontinued": true, "replacedBy": null }
]
} Output with Style set to positional record:
using System.Collections.Generic;
using System.Text.Json.Serialization;
public record Product(
[property: JsonPropertyName("sku")] string Sku,
[property: JsonPropertyName("price")] double Price,
[property: JsonPropertyName("stock")] long Stock,
[property: JsonPropertyName("tags")] List<string> Tags,
[property: JsonPropertyName("dimensions")] Dimensions Dimensions,
[property: JsonPropertyName("variants")] List<Variant> Variants
);
public record Variant(
[property: JsonPropertyName("color")] string Color,
[property: JsonPropertyName("stock")] long Stock,
[property: JsonPropertyName("discontinued")] bool? Discontinued,
[property: JsonPropertyName("replacedBy")] object? ReplacedBy
);
public record Dimensions(
[property: JsonPropertyName("width")] double Width,
[property: JsonPropertyName("height")] long Height
);
The property: target matters. Without it the attribute lands on the constructor parameter, which
System.Text.Json ignores, and the sku key no longer matches Sku. Discontinued was missing from one
variant, so it is bool?. ReplacedBy was only ever null, so it is
object?. STJ binds positional records through their constructor, which works from .NET 5.
Class style
The default style writes classes with { get; set; } auto-properties, one attribute per
property:
public class Dimensions
{
[JsonPropertyName("width")]
public double Width { get; set; }
[JsonPropertyName("height")]
public long Height { get; set; }
}
If a property would have the same name as its class, which C# doesn't allow (CS0542), it gets a
Value suffix. A name key inside a Name object becomes
NameValue.
Types to revisit
price is double because that's what JSON numbers are. For money, change it to
decimal; STJ reads JSON numbers into decimal without losing digits. Timestamps come
out as string; change them to DateTimeOffset to keep the UTC offset. Whole numbers are
long so large IDs fit, so narrow to int where you know the range. Empty JSON objects
become Dictionary<string, object>. No namespace is emitted, so add a file-scoped
namespace line yourself.
To validate the payload before it reaches your DTOs, generate a JSON Schema from the same sample.
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Go, Python, Rust, Java, JSON Schema.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.