JSON to C# Converter
Generate C# classes from JSON data. Paste your JSON and configure options:
Generate C# Classes from JSON
This tool automatically generates C# class definitions from your JSON data. It infers types from values, handles nested objects, and supports both Newtonsoft.Json and System.Text.Json serialization attributes.
Output Options
Standard POCO Classes
The default output generates plain old CLR object (POCO) classes with properties:
public class User
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}C# 9+ Records
Enable "Use Records" for immutable, concise data types. Records are ideal for DTOs and API response models:
public record User(string Name, int Age);Newtonsoft.Json vs System.Text.Json
Choose the serialization library your project uses:
- Newtonsoft.Json (default) — Uses
[JsonProperty]attributes. Most compatible, widely used in older projects. - System.Text.Json — Uses
[JsonPropertyName]attributes. Built into .NET Core 3.0+ and .NET 5+, better performance.
Type Inference
The converter automatically infers C# types from JSON values:
| JSON Type | C# Type | Notes |
|---|---|---|
| string | string | |
| integer | int or long | Uses long for values exceeding int range |
| decimal | double | |
| boolean | bool | |
| null | object? | Nullable reference type |
| array | List<T> | Type inferred from first element |
| object | Nested class | Generates separate class definition |
Example Conversion
Given this JSON:
{
"id": 1,
"email": "user@example.com",
"profile": {
"displayName": "John",
"avatarUrl": null
},
"roles": ["admin", "user"]
}The converter generates:
using Newtonsoft.Json;
using System.Collections.Generic;
public class Root
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("profile")]
public Profile Profile { get; set; }
[JsonProperty("roles")]
public List<string> Roles { get; set; }
}
public class Profile
{
[JsonProperty("displayName")]
public string DisplayName { get; set; }
[JsonProperty("avatarUrl")]
public object? AvatarUrl { get; set; }
}Using Generated Classes
Deserializing JSON with Newtonsoft.Json
using Newtonsoft.Json;
string json = GetJsonFromApi();
var user = JsonConvert.DeserializeObject<Root>(json);
Console.WriteLine(user.Email);Deserializing with System.Text.Json
using System.Text.Json;
string json = GetJsonFromApi();
var user = JsonSerializer.Deserialize<Root>(json);
Console.WriteLine(user.Email);Configuration Tips
- PascalCase conversion — Enable to convert
userNametoUserNamefollowing C# conventions - Namespace — Add a namespace wrapper for better code organization
- JSON attributes — Enable when property names differ from JSON keys, or disable for cleaner code when names match
Common Issues
Empty arrays
Empty arrays generate List<object> since the element type can't be inferred. Update to the correct type in your code.
Mixed-type arrays
If an array contains different types, the converter uses List<object>. Consider using separate properties or a discriminated union pattern.
Reserved keywords
If your JSON contains C# reserved keywords as keys (like "class" or "event"), you'll need to manually add the @ prefix to the property name.
Related Tools
- JSON Validator — Validate your JSON before converting
- JSON to TypeScript — Generate TypeScript interfaces
- JSON Schema Validator — Validate with a schema
Frequently Asked Questions
Should I use Newtonsoft.Json or System.Text.Json?
For new .NET 5+ projects, System.Text.Json is recommended — it's built-in and faster. Use Newtonsoft.Json for older projects or when you need its advanced features (custom converters, LINQ to JSON, etc.).
What's the difference between classes and records?
Records (C# 9+) are immutable by default and provide built-in value equality. They're ideal for DTOs. Classes are mutable and better for entities that change over time.
How do I handle nullable types?
The converter uses nullable reference types (e.g., string?) for null values. Enable nullable reference types in your project with<Nullable>enable</Nullable> in your .csproj file.
Can I add validation attributes?
The converter generates basic class structure only. Add [Required],[MaxLength], and other validation attributes manually as needed.