UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

JSON to CSharp Class

Convert JSON payload samples into C# class models with inferred nested types.

About JSON to CSharp Class

Manually writing C# classes from JSON responses wastes time and introduces typos — especially when APIs return deeply nested objects with dozens of fields. The JSON to C# Class converter analyzes any valid JSON object and instantly generates strongly-typed C# class definitions with correct property names, data types, and optional attributes like [JsonProperty] for Newtonsoft.Json or [JsonPropertyName] for System.Text.Json. Paste a JSON payload from a REST API, webhook, or configuration file and get production-ready C# models in seconds. The tool handles nested objects as inner classes, arrays as List<T> or T[] generics, nullable types for missing or null values, and Pascal-case naming conventions that match C# standards. It also infers numeric types, distinguishing int from long and double, and maps ISO date strings to DateTime. Whether you are consuming a third-party API,.

Why use JSON to CSharp Class

Accurate Type Inference

The converter distinguishes int, long, double, bool, string, DateTime, and Guid by inspecting actual values — not just defaulting everything to object or string. Numeric ranges, ISO timestamps, and UUID patterns are all detected automatically.

Newtonsoft & System.Text.Json Support

Toggle between [JsonProperty] for Newtonsoft.Json (Json.NET) and [JsonPropertyName] for the built-in System.Text.Json serializer, so generated classes drop straight into your target project without attribute changes.

Nested Object Flattening

Deeply nested JSON objects become properly structured inner classes or separate named classes, preserving the hierarchy while keeping the C# code idiomatic and maintainable rather than creating a wall of anonymous types.

Nullable Reference Type Support

For C# 8 and later projects with nullable context enabled, the tool emits string? and object? for properties that appear null or absent in the sample JSON, preventing null-reference warnings at compile time.

Pascal-Case Property Renaming

Snake_case and camelCase JSON keys are automatically converted to PascalCase C# property names while the original JSON key is preserved in the serialization attribute, matching both C# naming conventions and wire format.

List and Array Generic Detection

JSON arrays are mapped to List<T> with the element type inferred from the array contents, including arrays of objects that produce their own class definitions, so collection properties are strongly typed from the start.

How to use JSON to CSharp Class

  1. Paste your JSON object or array into the input field
  2. Choose your serialization attribute style: Newtonsoft.Json [JsonProperty] or System.Text.Json [JsonPropertyName]
  3. Select whether to emit nullable reference types (C# 8+ nullable context)
  4. Click Generate to produce the C# class definitions
  5. Copy the output and paste it into your Visual Studio or Rider project
  6. Rename the root class from RootObject to match your domain model name

When to use JSON to CSharp Class

  • When consuming a new REST API and needing strongly-typed response models quickly
  • When a third-party webhook payload arrives and you need C# deserialization classes
  • When prototyping a new feature that reads JSON configuration files
  • When onboarding onto a legacy codebase that still uses raw JObject instead of typed models
  • When writing integration tests and needing matching C# request/response contracts
  • When translating a TypeScript interface or OpenAPI schema into C# for a .NET backend

Examples

API response with nested object

Input: {"user_id": 42, "email": "dev@example.com", "created_at": "2024-01-15T09:00:00Z", "address": {"city": "Berlin", "zip": "10115"}}

Output: public class RootObject { [JsonProperty("user_id")] public int UserId { get; set; } [JsonProperty("email")] public string Email { get; set; } [JsonProperty("created_at")] public DateTime CreatedAt { get; set; } [JsonProperty("address")] public Address Address { get; set; } } public class Address { [JsonProperty("city")] public string City { get; set; } [JsonProperty("zip")] public string Zip { get; set; } }

Array of items

Input: [{"id": 1, "name": "Widget", "price": 9.99, "in_stock": true}]

Output: public class RootObject { [JsonProperty("id")] public int Id { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("price")] public double Price { get; set; } [JsonProperty("in_stock")] public bool InStock { get; set; } }

UUID and nullable field

Input: {"trace_id": "a3f1e2d4-bc09-4c71-8e55-123456789abc", "error": null, "count": 1500000000}

Output: public class RootObject { [JsonProperty("trace_id")] public Guid TraceId { get; set; } [JsonProperty("error")] public string? Error { get; set; } [JsonProperty("count")] public long Count { get; set; } }

Tips

  • Paste a real API response with populated values rather than an empty skeleton so the type inference engine has concrete data to analyze
  • After generating, rename RootObject and any generic Inner classes to meaningful domain names before committing to your codebase
  • If your JSON uses snake_case keys, the [JsonProperty] attribute preserves the original key name, so deserialization still works after the tool renames properties to PascalCase
  • For financial data, manually change inferred double properties to decimal after generation to avoid floating-point precision issues
  • Use the nullable output option even if your project targets .NET Framework — you can strip the ? annotations manually, but it helps identify which fields may be absent

Frequently Asked Questions

What C# serializer attributes does the tool support?
The tool supports both Newtonsoft.Json ([JsonProperty("key")]) and System.Text.Json ([JsonPropertyName("key")]). Choose the one matching your project's NuGet dependency. System.Text.Json is built into .NET 5 and later; Newtonsoft is still common in older or ASP.NET Core projects.
How does the tool handle JSON arrays at the root level?
If the root JSON value is an array, the tool inspects the first element to determine the item type and wraps the whole thing in a List<RootObject>. You can then rename RootObject to match your domain entity.
Will it generate nullable types for missing properties?
Yes, when nullable reference types are enabled. Properties whose values are null in the sample JSON are emitted as string? or ClassName? rather than string or ClassName. This requires C# 8 or later with <Nullable>enable</Nullable> in your .csproj.
How are JSON numbers mapped to C# numeric types?
Integer values within the int range become int; values outside that range become long. Numbers with decimal points become double. If you need decimal for financial values, you will need to manually change those properties after copying the output.
Does the tool handle ISO 8601 date strings as DateTime?
Yes. Strings matching ISO 8601 format (e.g., 2024-06-15T10:30:00Z) are inferred as DateTime rather than string, which matches what JSON.NET and System.Text.Json deserialize them to by default.
Can I use the output directly in ASP.NET Core?
Yes. The generated classes are plain C# POCOs with no framework dependencies beyond the serialization attribute namespace. They work with model binding, Swagger schema generation, and unit tests without modification.
What happens if two nested objects have the same shape?
The tool generates separate classes for each unique JSON key path, so two objects named address under different parent keys will produce two class definitions. You can manually merge them into one class if the shapes are identical.
Does the tool handle GUIDs and UUIDs?
Yes. Strings matching the standard UUID/GUID pattern (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) are mapped to Guid rather than string, which integrates cleanly with .NET's Guid type and EF Core model mappings.

Explore the category

Glossary

POCO
Plain Old CLR Object — a simple C# class with no framework inheritance or attributes required. JSON deserialization targets are typically POCOs.
JsonProperty
A Newtonsoft.Json attribute that maps a C# property name to its JSON key, allowing PascalCase properties to serialize to/from camelCase or snake_case wire format.
Nullable Reference Types
A C# 8 feature enabled via <Nullable>enable</Nullable> that distinguishes string (non-null guaranteed) from string? (may be null) at compile time, reducing NullReferenceExceptions.
System.Text.Json
The built-in .NET JSON serializer introduced in .NET Core 3.0, offering better performance than Newtonsoft.Json with a slightly stricter API surface.
Type Inference
The process of determining a variable's or property's data type from its value rather than an explicit annotation — used here to map JSON values to appropriate C# primitive types.
Deserialization
Converting a JSON string into a populated C# object graph, the reverse of serialization. Requires class definitions whose properties match the JSON structure.