"category": "code-generator",
JSON to C# Class Generator
"tldr": Paste JSON and get C# classes with PascalCase properties and JsonPropertyName attributes for System.Text.Json.
Convert JSON into C# classes with PascalCase properties and [JsonPropertyName] attributes, ready for System.Text.Json. Nested objects become their own classes and arrays become List<T> with inferred element types.
The attributes preserve the original JSON key spelling, so deserialization works for snake_case and camelCase APIs without any global naming-policy configuration - and the classes also work with Newtonsoft.Json by swapping the attribute.
How to convert JSON to C# classes
- 1Paste a representative JSON payload into the input panel.
- 2Properties are PascalCased; [JsonPropertyName] keeps the original key for correct mapping.
- 3Nested objects become separate classes; arrays become List<T>.
- 4Copy the classes into your project and deserialize with JsonSerializer.Deserialize<Root>().
Convert JSON to C# class in code
using System.Text.Json;
var order = JsonSerializer.Deserialize<Root>(json);
Console.WriteLine(order?.Customer.Name);using System.Net.Http.Json;
var order = await httpClient.GetFromJsonAsync<Root>("/api/orders/1042");Frequently asked questions
System.Text.Json or Newtonsoft.Json - does the output work with both?
The generated classes target System.Text.Json ([JsonPropertyName]). For Newtonsoft.Json, replace the attribute with [JsonProperty("key")] - the class structure is otherwise identical. New .NET projects should default to System.Text.Json.
How do I make properties nullable?
Append ? to the type: string?, long?, Customer?. With nullable reference types enabled (default in modern .NET), mark every property that can legitimately be null or absent - the compiler then forces you to handle those cases.
Can I use records instead of classes?
Yes - System.Text.Json supports records: record Customer([property: JsonPropertyName("name")] string Name). Records give value equality and immutability; convert the generated classes if that fits your style.
Why long instead of int for whole numbers?
JSON does not constrain number size, and IDs or timestamps routinely exceed int.MaxValue (2.1 billion). long avoids silent overflow; narrow to int only for fields you know are small.
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →