Skip to content

JSON to C# Class Generator - Classes and Records

Input JSON
C# Output

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

Frequently Asked Questions

Does this work with Newtonsoft.Json?
The attributes are System.Text.Json's `[JsonPropertyName]`, which Newtonsoft ignores. Newtonsoft matches property names case-insensitively, so `Sku` still binds to `sku`, but a key like `replaced_by` wouldn't bind to `ReplacedBy`. For Newtonsoft, find and replace the attribute with `[JsonProperty("...")]` and the `using` with `Newtonsoft.Json`.
Why do I get CS8618 warnings on the classes?
With `<Nullable>enable</Nullable>`, a non-nullable `string` or `List<string>` property with no initializer triggers CS8618. Mark it `required` (C# 11), give it a default such as `= "";`, or use the record style, where constructor parameters don't raise that warning.
What does System.Text.Json do with a missing property?
It leaves the default: `null` for reference types, `0` for `long`, `false` for `bool`. That's why keys missing from some samples get `?`, so you can tell 'not sent' from zero. If a key must be present, add the `required` modifier; from .NET 7, deserialization throws when a required property is missing.
Why `object?` for some properties?
The value was null in every sample, or it changed type between records, so there is no single type to infer. With STJ, an `object` property holds a `JsonElement` after deserialization. Replace it with the real type once you know it.
Isn't this what Visual Studio's Paste JSON as Classes does?
Similar idea. Paste Special in Visual Studio looks at the JSON you paste but keeps the JSON's own casing for property names and adds no `JsonPropertyName` attributes or nullable annotations. This tool merges array items into one type and marks fields that were missing or null.