JSON to C# class
The tool writes a class per object, ready to use with JsonSerializer.Deserialize in .NET.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter json input and root type name.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
string, int or long, double, bool, List<T> and object?, with a public class per object. Each property is PascalCase with [JsonPropertyName] holding the original key. null or a missing key makes the type nullable, such as int? or string?, and required strings and lists start empty.
Example
Inputs: Root type name = Root. Result: Generated code: #nullable enable using System.Collections.Generic; using System.Text.Json.Serialization; public class Root { [JsonPropertyName("id")] public int Id { get; set; } [JsonPropertyName("name")] public string Name { get; set; } = ""; [JsonPropertyName("email")] public string Email { get; set; } = ""; [JsonPropertyName("is_active")] public bool IsActive { get; set; } [JsonPropertyName("balance")] public double Balance { get; set; } [JsonPropertyName("tags")] public List<string> Tags { get; set; } = new(); [JsonPropertyName("address")] public Address Address { get; set; } = new(); [JsonPropertyName("orders")] public List<Order> Orders { get; set; } = new(); } public class Address { [JsonPropertyName("city")] public string City { get; set; } = ""; [JsonPropertyName("postal_code")] public string PostalCode { get; set; } = ""; } public class Order { [JsonPropertyName("order_id")] public string OrderId { get; set; } = ""; [JsonPropertyName("total")] public double Total { get; set; } [JsonPropertyName("coupon")] public string? Coupon { get; set; } }; Types generated: 3; Fields: 13.
{
"id": 1024,
"name": "Sara Ahmed",
"email": "sara@example.com",
"is_active": true,
"balance": 1520.75,
"tags": ["vip", "riyadh"],
"address": { "city": "Riyadh", "postal_code": "12211" },
"orders": [
{ "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
{ "order_id": "A-2", "total": 120, "coupon": null }
]
}#nullable enable
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } = "";
[JsonPropertyName("email")]
public string Email { get; set; } = "";
[JsonPropertyName("is_active")]
public bool IsActive { get; set; }
[JsonPropertyName("balance")]
public double Balance { get; set; }
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = new();
[JsonPropertyName("address")]
public Address Address { get; set; } = new();
[JsonPropertyName("orders")]
public List<Order> Orders { get; set; } = new();
}
public class Address
{
[JsonPropertyName("city")]
public string City { get; set; } = "";
[JsonPropertyName("postal_code")]
public string PostalCode { get; set; } = "";
}
public class Order
{
[JsonPropertyName("order_id")]
public string OrderId { get; set; } = "";
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("coupon")]
public string? Coupon { get; set; }
}Limits and caveats
- It uses System.Text.Json, built into .NET; with Newtonsoft.Json swap the attribute for [JsonProperty].
- The = new(); initializer needs C# 9 or newer.
Questions about this tool
Why do some types end with ?
It means the value can be null. It appears for keys that were null or missing in some array items.
Why was a property renamed RootValue?
C# does not allow a member with the same name as its class, so Value is added and the attribute keeps the original key.
Is my JSON sent to a server?
No. The conversion runs in your browser and the JSON never leaves your device.
