JSON to C# Class Generator
Paste JSON and get C# classes with [JsonPropertyName] attributes, ready for System.Text.Json. Runs locally in your browser.
Paste JSON and get C# classes with [JsonPropertyName] attributes, ready for System.Text.Json. Runs locally in your browser.
Paste a JSON sample, for example an API response, and click Generate. This JSON to C# class generator turns every JSON object into a C# class with auto-properties. Property names are converted to PascalCase, and the original key is kept in a [JsonPropertyName] attribute, so System.Text.Json deserializes your data without a custom naming policy. The output starts with the two using directives it needs and #nullable enable, and targets .NET 6, 7 and 8.
.cs file. Add a namespace line and rename Root to your model name.JsonSerializer.Deserialize<Root>(json).| JSON value | C# type |
|---|---|
42 | long |
9.5, or a mix of 2 and 2.5 across items | double |
"text" | string, initialised to string.Empty |
true | bool |
{ ... } | A new class named after the key, initialised with default! |
[ ... ] | List<T>, initialised to new() |
null next to a real value | Nullable type such as double? |
null everywhere | object? |
| Key missing from some array items | Nullable type such as string? |
{"order_id":1042,"customer":{"name":"Ada","is_vip":true},"total":99.9,
"lines":[{"sku":"A1","qty":2}],"coupon":null}
generates:
using System.Collections.Generic;
using System.Text.Json.Serialization;
#nullable enable
public class Root
{
[JsonPropertyName("order_id")]
public long OrderId { get; set; }
[JsonPropertyName("customer")]
public Customer Customer { get; set; } = default!;
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("lines")]
public List<Line> Lines { get; set; } = new();
[JsonPropertyName("coupon")]
public object? Coupon { get; set; }
}
public class Customer
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("is_vip")]
public bool IsVip { get; set; }
}
public class Line
{
[JsonPropertyName("sku")]
public string Sku { get; set; } = string.Empty;
[JsonPropertyName("qty")]
public long Qty { get; set; }
}
lines becomes List<Line> because array classes take the singular of the key. coupon is object? because the sample only shows null; change it to string? or a Coupon class once you know its real shape.
{ "items": [
{ "id": 1, "note": "a", "score": null },
{ "id": 2, "score": 3.5, "created_at": "2026-01-01" } ] }
generates a Root class with List<Item> Items and this Item class. note and created_at are missing from one item, so they become nullable, and score is nullable because one item has null:
public class Item
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("note")]
public string? Note { get; set; }
[JsonPropertyName("score")]
public double? Score { get; set; }
[JsonPropertyName("created_at")]
public string? CreatedAt { get; set; }
}
When the JSON starts with [, the generator describes the elements and adds a comment saying so:
// The top-level value is an array; the classes below describe its elements.
public class Root { ... }
// usage
var rows = JsonSerializer.Deserialize<List<Root>>(json);
N prefix: "2fa" becomes N2fa, still mapped by [JsonPropertyName("2fa")].user_id and userId, the second gets a trailing underscore. A property with the same name as its class gets a Value suffix, because C# forbids that.long, even small ones. Narrow to int where you know the range, or keep long for IDs and Unix timestamps.string. Change them to DateTime or DateTimeOffset if the format is ISO 8601; System.Text.Json parses that natively.[1, "a"] become List<object>. System.Text.Json fills those with JsonElement values, so switch the type to List<JsonElement> to make that explicit, or write a custom converter.If your source data is XML or YAML, use XML to C# or YAML to C#. The same JSON can also be turned into Java classes, TypeScript interfaces or Go structs, and all languages are available in the class generator hub. To tidy hand-edited C# afterwards, use the C# formatter, and to validate payloads at run time against a contract, generate a schema with the JSON Schema generator.
Yes, after one change: replace [JsonPropertyName("x")] with [JsonProperty("x")] and using System.Text.Json.Serialization; with using Newtonsoft.Json;. The class shapes are identical.
The generator emits classes with setters because they work with every serializer setting. To use records, change public class Item to public record Item; the properties stay valid.
It is similar, but Visual Studio does not add [JsonPropertyName] attributes, so snake_case keys need a naming policy. This tool also works without an IDE.
The value was null in every place it appeared, so there was nothing to infer a type from. Add a real value to the sample or set the type by hand.
No. The JSON is parsed and the C# is generated in your browser; nothing is sent to a server.