JSON to C# Class Generator — Free Online Tool
Convert JSON to C# class definitions with proper types, nullable annotations, and JsonProperty attributes using AI. Free, no sign-up.
JSON to C# Class Generator — Free Online Tool
About JSON to C# Class Generator — Free Online Tool
JSON to C# Class converts JSON objects into strongly-typed C# class definitions, automatically adding Newtonsoft.Json JsonProperty attributes and System.Text.Json JsonPropertyName attributes to map JSON keys to C# property names. Developers use it to eliminate hand-writing data transfer objects (DTOs), avoid typos in property mappings, and instantly scaffold model classes when consuming REST APIs or deserializing configuration files.
JSON to C# Type Mapping
| JSON Type | JSON Example | C# Type | Notes |
|---|---|---|---|
| string | "hello" | string | Nullable as string? in C# 8+ nullable contexts |
| number (integer) | 42 | int | Long integers map to long; tool infers from value range |
| number (float) | 3.14 | double | Use decimal for financial data requiring exact precision |
| boolean | true | bool | Nullable variant is bool? |
| null | null | object? | Or typed nullable e.g. string? when sibling values reveal type |
| object | {"id":1} | class (nested) | Generates a new nested class definition |
| array of objects | [{"id":1}] | List<T> | T is the generated nested class type |
| array of primitives | [1,2,3] | List<int> | Element type matches JSON primitive mapping |
| empty array | [] | List<object> | Falls back to object when element type cannot be inferred |
| date string | "2024-01-15" | string or DateTime | DateTime when format matches ISO 8601; string otherwise |
Deserialization Library Attribute Comparison
| Feature | Newtonsoft.Json (Json.NET) | System.Text.Json (.NET 6+) | ServiceStack.Text |
|---|---|---|---|
| Property name attribute | [JsonProperty("key")] | [JsonPropertyName("key")] | [DataMember(Name="key")] |
| Ignore property | [JsonIgnore] | [JsonIgnore] | [IgnoreDataMember] |
| Required property | [JsonProperty(Required = Required.Always)] | [JsonRequired] (.NET 7+) | N/A |
| Custom converter | [JsonConverter(typeof(T))] | [JsonConverter(typeof(T))] | [JsConfig] static config |
| Null handling | NullValueHandling.Ignore | JsonIgnoreCondition.WhenWritingNull | JsConfig.IncludeNullValues |
| Camel-case default | CamelCasePropertyNamesContractResolver | JsonSerializerOptions.PropertyNamingPolicy | JsConfig.EmitCamelCaseNames |
| NuGet package | Newtonsoft.Json | Built into .NET 6+; System.Text.Json on older | ServiceStack.Text |
| .NET version support | .NET Framework 2.0+ | .NET Core 3.0+ / .NET 5+ | .NET Framework 3.5+ |
Frequently Asked Questions
How do I deserialize JSON to a C# class using Newtonsoft.Json?
Install the Newtonsoft.Json NuGet package, paste your generated class, then call JsonConvert.DeserializeObject<YourClass>(jsonString). The [JsonProperty] attributes on each property tell the deserializer how to map JSON keys that differ from the C# property names. For lists, use JsonConvert.DeserializeObject<List<YourClass>>(jsonString) when the root JSON value is an array.
What is the difference between JsonProperty and JsonPropertyName in C#?
[JsonProperty] belongs to Newtonsoft.Json (Json.NET) and has been the standard since .NET Framework days, supporting rich options like NullValueHandling and Required. [JsonPropertyName] belongs to System.Text.Json, the built-in serializer introduced in .NET Core 3.0 and the recommended choice for new .NET 6+ projects. Both attributes serve the same core purpose of mapping a JSON key name to a C# property name when they differ.
How do I handle nullable properties when converting JSON to C# class?
Enable C# 8+ nullable reference types in your project (add <Nullable>enable</Nullable> to your .csproj) and mark properties that can be null with a trailing ? (e.g., string? Description). For value types like int, use int? to allow null. With System.Text.Json, add [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] to omit null properties during serialization.
Can I convert nested JSON objects to C# classes automatically?
Yes — nested JSON objects generate separate nested class definitions, and the parent class holds a property typed to that child class. For example, a JSON object with an address field containing street and city keys produces both a root class with an Address property and a separate Address class. Arrays of objects generate a List<ChildClass> property and the corresponding child class definition.
Why does my JSON to C# conversion use object instead of a specific type?
Generators fall back to object when a JSON value is null and there are no other array elements or sibling values to infer the type from. To fix this, provide a sample JSON payload that includes a non-null representative value for those fields. Alternatively, manually replace object or object? with the correct type (e.g., string?, int?, List<OrderItem>) after generation.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.