JSON to C# Class Generator

JSON Input
1
Root Class
C# Class Output

Last updated:

Jsonic's JSON to C# Class generator converts a JSON object into C# class definitions using System.Text.Json conventions. Each property gets a [JsonPropertyName("key")] attribute to preserve the original JSON key. JSON strings map to string, integers to long, decimals to double, booleans to bool. Null values produce nullable types (string?, long?). JSON arrays become List<T> with the appropriate using System.Collections.Generic import. Nested objects generate separate named classes declared before the root class. All processing runs in your browser with no data upload.

How to convert JSON to a C# class

  1. Paste your JSON object into the left panel, or click Example to load a sample.
  2. Optionally change the root class name (default: Root).
  3. Click Generate.
  4. Copy the generated C# code into your project.
  5. Deserialize with: var obj = JsonSerializer.Deserialize<Root>(jsonString);

FAQ

Which JSON library does the generated code use?

The generated code uses System.Text.Json, which is built into .NET 5+ and .NET Core 3.0+. It uses [JsonPropertyName] attributes from System.Text.Json.Serialization. For Newtonsoft.Json (Json.NET), replace [JsonPropertyName] with [JsonProperty] and add using Newtonsoft.Json.

How are JSON types mapped to C# types?

JSON strings → string, JSON integers → long, JSON decimals → double, JSON booleans → bool, JSON null → nullable type (string?, long?, bool?), JSON arrays → List<T>, JSON objects → named class. The long type (64-bit integer) is used instead of int to safely handle large JSON numbers.

How are nullable fields handled?

Fields with null values in the JSON get nullable C# types: string?, long?, double?, bool?. System.Text.Json sets nullable properties to null when the JSON field is missing or null. Enable nullable reference types in your .csproj with <Nullable>enable</Nullable> to get full null-safety.

What is [JsonPropertyName] used for?

[JsonPropertyName("key")] tells System.Text.Json which JSON key maps to this C# property. Without it, System.Text.Json uses case-insensitive property name matching by default. Explicit attributes are more reliable and avoid ambiguity when JSON keys use camelCase and C# properties use PascalCase.

How do I deserialize JSON with the generated class?

Use JsonSerializer.Deserialize<Root>(jsonString) from System.Text.Json. For async streams: await JsonSerializer.DeserializeAsync<Root>(stream). For ASP.NET Core Web API, the generated class works directly as a controller action parameter — the framework deserializes the request body automatically.

Can I use this with ASP.NET Core Web API?

Yes. The generated classes with [JsonPropertyName] attributes work directly as ASP.NET Core request/response models. Use them as action parameters ([FromBody] Root model) or return types. The [JsonPropertyName] attributes ensure the JSON field names match regardless of property naming conventions.

How are nested objects handled?

Each nested JSON object becomes a separate C# class. Nested classes are declared before the root class in the output. The root class references the nested class by type name (e.g., public Address Address { get; set; }).

Should I use records instead of classes?

For immutable data, replace public class Foo with public record Foo and { get; set; } with { get; init; }. Records support value equality and are a good fit for DTOs. The generated class syntax is valid for both classes and records — change the keyword manually after generating.