JSON to C# class: from JSON to code
Paste JSON and get C# classes with auto-properties and the attribute that maps each one to its key: [JsonPropertyName] for System.Text.Json (the default) or [JsonProperty] for Newtonsoft.Json. Names become PascalCase, numbers int or double, lists List<T>, and a value that may be missing or null gets a nullable type (T?).
This page opens the converter on C#. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.
A real conversion
The tool opens with this JSON, an order with a customer and two items:
{
"id": 48213,
"status": "paid",
"paid_at": "2026-11-21T14:03:00Z",
"total": 389.3,
"coupon": null,
"customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
"items": [
{ "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
{ "sku": "MS-07", "qty": 1, "price": 89.5 }
]
}and this is what it writes for C#:
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("status")]
public string Status { get; set; }
[JsonPropertyName("paid_at")]
public string PaidAt { get; set; }
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("coupon")]
public object? Coupon { get; set; }
[JsonPropertyName("customer")]
public Customer Customer { get; set; }
[JsonPropertyName("items")]
public List<Item> Items { get; set; }
}
public class Customer
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("vip")]
public bool Vip { get; set; }
}
public class Item
{
[JsonPropertyName("sku")]
public string Sku { get; set; }
[JsonPropertyName("qty")]
public int Qty { get; set; }
[JsonPropertyName("price")]
public double Price { get; set; }
[JsonPropertyName("tags")]
public List<string>? Tags { get; set; }
}How each JSON value is typed
| In the JSON | In C# |
|---|---|
| string | string |
| integer | int |
| decimal | double |
| true / false | bool |
| null | object? |
| array of items | List<Item> |
| field missing in some items | List<string>? |
The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.
Awkward keys, null and empty lists
{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Account
{
[JsonPropertyName("user_id")]
public int UserId { get; set; }
[JsonPropertyName("display name")]
public string DisplayName { get; set; }
[JsonPropertyName("tags")]
public List<object> Tags { get; set; }
[JsonPropertyName("address")]
public object? Address { get; set; }
[JsonPropertyName("scores")]
public List<double> Scores { get; set; }
[JsonPropertyName("meta")]
public Meta Meta { get; set; }
[JsonPropertyName("mixed")]
public List<object> Mixed { get; set; }
}
public class Meta
{
[JsonPropertyName("2fa")]
public bool Field2fa { get; set; }
}"user_id" becomes UserId, "display name" DisplayName, "2fa" Field2fa (a C# name cannot start with a digit), and the original key stays in the attribute. null and the empty array become object? and List<object>.
The other flavour
The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "newtonsoft" one:
using System.Text.Json.Serialization;
public class Order
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("paid_at")]
public string PaidAt { get; set; }
[JsonPropertyName("coupon")]
public object? Coupon { get; set; }
}using Newtonsoft.Json;
public class Order
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("paid_at")]
public string PaidAt { get; set; }
[JsonProperty("coupon")]
public object? Coupon { get; set; }
}The Newtonsoft flavour swaps the attributes and the using for the Newtonsoft.Json ones; the classes are otherwise the same.
Deserializing with System.Text.Json
var order = JsonSerializer.Deserialize<Root>(json);
Console.WriteLine(order?.Customer.Name);System.Text.Json is case-sensitive by default, which is why every property carries its [JsonPropertyName]. With Newtonsoft.Json choose that flavour and call JsonConvert.DeserializeObject<Root>(json).
Common mistakes
- With nullable reference types on, non-nullable string properties without a value give compiler warnings. Add the required modifier, a default or make them nullable, depending on what the API guarantees.
- Using double for money: "total": 389.3 is exact in JSON but not in binary floating point. Use decimal for amounts.
How to use the converter
- Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
- Set the root name and the flavour; the code updates as you type.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert JSON to a C# class?
Paste the JSON, choose System.Text.Json or Newtonsoft and the root name, and copy the classes into your project. Then deserialize.
Should I use a class or a record?
The tool writes classes with get and set. If you want immutability, change them to records or add init accessors by hand; the property names and attributes stay the same.
Why is the root class called Root?
A JSON document has no name, so the tool needs one: type your own in the root name field (Order, Invoice…) and every nested class gets its name from its key.