JSON to C# Class

Generate C# classes from JSON

Try an example
Root class name:

JSON Input

C# Output

Runs locally Β· Safe to paste secrets
C# classes will appear here…

What is JSON to C# Conversion?

JSON to C# conversion is the process of generating strongly-typed C# class definitions from JSON data. When a .NET application receives JSON from an API, configuration file, or message queue, it needs C# classes to deserialize that data into objects. Writing these classes by hand is tedious and error-prone, especially when the JSON contains nested objects, arrays, and mixed types.

A JSON to C# class generator inspects the structure and values in your JSON, infers the appropriate C# type for each field, and produces class definitions with property accessors and serialization attributes. The output follows .NET naming conventions: PascalCase property names, JsonPropertyName attributes for mapping to the original camelCase or snake_case JSON keys, and correct use of List'<'T'>' for arrays.

This conversion is especially common in ASP.NET Core web APIs, Azure Functions, and any .NET service that consumes external JSON data. Rather than using dynamic or Dictionary'<'string, object'>', typed classes give you compile-time safety, IntelliSense support, and protection against field name typos that would otherwise only surface at runtime. Paste your JSON into this tool and get ready-to-use C# classes instantly.

Why Use a JSON to C# Generator?

Manually writing C# model classes from JSON samples takes time and introduces human error. A generator handles the repetitive parts so you can focus on business logic.

⚑
Instant class generation
Paste your JSON and get complete C# classes in under a second. No manual property typing, no forgetting a field.
πŸ”’
Privacy-first processing
All conversion runs in your browser. Your JSON data never leaves your machine, so API keys, tokens, and PII stay private.
πŸ“‹
Correct attribute mapping
Generated classes include JsonPropertyName attributes that map PascalCase C# properties to the original JSON key names, preventing deserialization mismatches.
🌐
No account or install required
Open the page and paste your JSON. No NuGet packages to install, no Visual Studio extensions to configure, no sign-up forms.

JSON to C# Use Cases

Backend API Integration
Generate request and response models for third-party REST APIs. Paste the sample JSON from API documentation and get ready-to-use C# classes for HttpClient calls.
ASP.NET Core Controllers
Create model classes for action method parameters. When your endpoint accepts a JSON body, the generated class gives you automatic model binding and validation.
Azure Functions & Serverless
Build input and output bindings for Azure Functions that process JSON messages from queues, event hubs, or HTTP triggers.
Configuration Deserialization
Convert appsettings.json sections or external config files into typed C# options classes for use with IOptions'<'T'>' in dependency injection.
Database Seeding & Migration
Turn JSON fixture files into C# objects for Entity Framework seed data, integration test setups, or data migration scripts.
Learning C# Type Systems
Students and developers new to .NET can see how JSON types map to C# types, understand property accessors, and learn about serialization attributes by example.

JSON to C# Type Mapping Reference

Each JSON value type maps to a specific C# type. The generator analyzes actual values to pick the most precise type. For example, a JSON number with no decimal point becomes int, while 3.14 becomes double. Null values produce object? (nullable reference type) since the actual type cannot be inferred from null alone.

JSON TypeExampleC# Type
string"hello"string
number (int)42int
number (float)3.14double
booleantrue, falsebool
nullnullobject?
object{"key": "value"}nested class
array[1, 2, 3]List<int>
array of objects[{"id": 1}]List<ClassName>

System.Text.Json vs Newtonsoft.Json

.NET has two major JSON serialization libraries. The generated classes from this tool use System.Text.Json attributes (JsonPropertyName), which is the built-in option since .NET Core 3.0. If your project uses Newtonsoft.Json, you can swap the attributes.

System.Text.Json
Built into .NET since Core 3.0. Faster and lower memory allocation than Newtonsoft. Uses [JsonPropertyName] for property mapping. The default choice for new .NET 6+ projects. Does not support all Newtonsoft features like JsonPath or reference handling out of the box.
Newtonsoft.Json
The original .NET JSON library (Json.NET). Uses [JsonProperty] for property mapping. Supports more advanced scenarios: polymorphic serialization, JsonPath queries, custom converters with more flexibility. Still common in legacy codebases and projects that need features System.Text.Json lacks.

Code Examples

These examples show how to define and use C# classes generated from JSON, using both major serialization libraries, plus how to automate generation from the command line.

C# (System.Text.Json)
using System.Text.Json;

var json = """{"id": 1, "name": "Alice", "active": true}""";
var user = JsonSerializer.Deserialize<User>(json);
Console.WriteLine(user.Name); // β†’ "Alice"

public class User
{
    [JsonPropertyName("id")]
    public int Id { get; set; }

    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("active")]
    public bool Active { get; set; }
}
C# (Newtonsoft.Json)
using Newtonsoft.Json;

var json = @"{""id"": 1, ""name"": ""Alice"", ""scores"": [98, 85]}";
var user = JsonConvert.DeserializeObject<User>(json);
Console.WriteLine(user.Scores[0]); // β†’ 98

public class User
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("scores")]
    public List<int> Scores { get; set; }
}
JavaScript (quicktype)
// Install: npm install -g quicktype
// Generate C# classes from JSON:
// quicktype --lang csharp --src data.json --out Models.cs

// From stdin:
// echo '{"id": 1, "name": "Alice"}' | quicktype --lang csharp

// Output:
// public class Root
// {
//     [JsonPropertyName("id")]
//     public int Id { get; set; }
//
//     [JsonPropertyName("name")]
//     public string Name { get; set; }
// }
Python (generate C# from JSON)
import json

def json_to_csharp(data: dict, class_name: str = "Root") -> str:
    lines = [f"public class {class_name}", "{"]
    type_map = {str: "string", int: "int", float: "double", bool: "bool"}

    for key, value in data.items():
        cs_type = type_map.get(type(value), "object")
        prop = key[0].upper() + key[1:]  # PascalCase
        lines.append(f'    [JsonPropertyName("{key}")]')
        lines.append(f'    public {cs_type} {prop} {{ get; set; }}')
        lines.append('')
    lines.append("}")
    return "\n".join(lines)

data = json.loads('{"id": 1, "name": "Alice", "active": true}')
print(json_to_csharp(data))

Frequently Asked Questions

What is the difference between JsonPropertyName and JsonProperty?
JsonPropertyName is the attribute from System.Text.Json, the built-in .NET serializer. JsonProperty is from Newtonsoft.Json (Json.NET), a third-party library. Both map a C# property name to a JSON field name, but they belong to different namespaces and are not interchangeable. Use JsonPropertyName for new .NET 6+ projects and JsonProperty if your project already depends on Newtonsoft.
How does the generator handle nested JSON objects?
Each nested object becomes its own C# class. The property in the parent class uses the nested class as its type. For example, if your JSON has an "address" field containing an object with "street" and "city", the generator creates an Address class and types the property as Address in the parent class.
What happens with JSON arrays that contain mixed types?
The generator infers the array element type from the first non-null element. If the array contains mixed types (strings and numbers together), the generated type falls back to List'<'object'>'. In practice, well-structured API responses rarely mix types within a single array.
Can I use the generated classes with Entity Framework?
Yes, but you will need to add EF-specific annotations like [Key], [Required], or [Column] attributes. The generated classes give you the property structure; treat them as a scaffold and add EF configuration on top. EF also expects a parameterless constructor, which the generated classes already have by default.
How are null values in JSON converted to C#?
A JSON null value produces the type object? because the generator cannot determine the intended type from null alone. After generation, you should replace object? with the correct nullable type (string?, int?, or a specific class?) based on your knowledge of the API schema.
Is camelCase JSON automatically mapped to PascalCase C# properties?
Yes. The generator converts JSON keys like "firstName" to PascalCase properties like FirstName and adds a [JsonPropertyName("firstName")] attribute. This ensures the serializer maps between the two naming conventions automatically during serialization and deserialization.
How do I handle JSON with optional fields in C#?
Make the property type nullable (e.g., string? or int?) and set a default value. With System.Text.Json, you can also configure JsonSerializerOptions.DefaultIgnoreCondition to skip null values during serialization. The generator produces non-nullable types by default, so review and adjust nullability based on your API contract.