JSON to C# Converter
Backend developers wiring up a new API client, integration engineers mapping a third-party webhook, and anyone writing a DTO by hand all hit the same tedious wall: transcribing a JSON sample into a C# model, one property at a time, guessing whether a field is an int or a long, and forgetting the serializer attribute until the binding fails at runtime.
Paste your JSON and this converter emits a complete C# class or record for every nested object, with PascalCase properties, correct primitive types, and the serializer attributes your project uses. It runs entirely in the tab, so sample payloads containing internal IDs or customer data never leave the machine.
How the conversion works
The converter walks the parsed JSON tree and maps each value to a C# type. Whole numbers become int when they fit in 32 bits and long when they do not. Decimal numbers become double. JSON true and false become bool, and any non-numeric value becomes string. Arrays produce a List of the element type, and every distinct object shape becomes its own class so nesting is preserved instead of collapsed into one giant type.
Property names are converted to PascalCase and any C# keyword collision is escaped with the @ prefix. When serializer attributes are enabled, each property is annotated with the original JSON key so deserialization binds correctly regardless of casing. With nullable reference types on, reference-type properties get a trailing ? unless the sample shows a concrete non-null value.
The generated code is plain C# with no third-party dependencies beyond the serializer you selected. Paste it into a new file, add the matching using directive, and compile.
Before and after: a typical API response
Input, a JSON payload from a user endpoint:
{ "id": 42, "name": "Ada Lovelace", "active": true,
"tags": ["math", "code"],
"address": { "city": "London" } } Output, System.Text.Json class with nullable reference types:
using System.Text.Json.Serialization;
using System.Collections.Generic;
namespace MyApp.Models
{
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("active")]
public bool Active { get; set; }
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = new();
[JsonPropertyName("address")]
public Address? Address { get; set; }
}
public class Address
{
[JsonPropertyName("city")]
public string? City { get; set; }
}
} Who converts JSON to C# with this tool
Developers
- Generate a DTO class for a third-party REST response without hand-typing every property.
- Spin up a typed client model when consuming a new webhook format mid-sprint.
- Produce a record for a request body in a minimal API and start compiling immediately.
Production
- Regenerate models after a vendor changes their API and diff the output against the old class.
- Enforce a consistent serializer attribute style across a microservices codebase.
- Validate that nullable annotations match the real payload before enabling nullable in a project.
Workflow
- Convert the same JSON to C# and TypeScript so backend and frontend share identical field names.
- Paste the generated class into a scratch file, compile once, then move it into the real project.
- Pair with the JSON validator to confirm the payload parses before generating the model.
Common mistakes when generating C# from JSON
- Ignoring the serializer attribute mismatch. If your project uses Newtonsoft but you generate System.Text.Json attributes, every property silently binds to null at runtime because the casing does not match. Match the attribute style to the serializer already registered in Program.cs or Startup.cs.
- Assuming every number is an int. Large IDs and timestamps overflow int and need long. The converter picks long automatically when the sample value exceeds int.MaxValue, but a sample with a small ID will still emit int. If you know the field grows, switch it to long by hand.
- Deleting the @ prefix on keyword properties. A JSON field named class or event produces @class or @event in C#. Removing the @ makes the file fail to compile. Keep the prefix or rename the property and rely on the attribute for binding.
- Generating mutable classes when you wanted immutability. Records give value equality and immutability by default. If you pick class by habit and then add init-only accessors manually, you are doing the work the record keyword already does. Switch the Type dropdown to record.
Best practices for JSON to C# workflows
- 1.Paste a realistic sample with real nesting and at least one array so every class the converter emits is exercised by your actual data shape.
- 2.Match the serializer attributes to the library your project already references, and confirm with one round-trip deserialization test before committing.
- 3.Prefer records for immutable DTOs on .NET 6 and later, and enable nullable reference types so missing fields surface at compile time instead of as null references.
- 4.Regenerate and diff when the upstream API changes, rather than editing properties by hand, so new fields are not silently dropped.
Frequently asked questions
How does this JSON to C# converter work?
The tool parses your JSON in the browser, infers a C# type for every field (int, long, double, bool, string, arrays, and nested objects), and emits a class or record for each distinct object shape. Properties are converted to PascalCase, nested objects become their own classes, and repeated array element types are merged into a single element class. You choose whether to emit System.Text.Json [JsonPropertyName] attributes, Newtonsoft [JsonProperty] attributes, or no attributes at all.
What is the difference between a class and a record in C#?
A class is the traditional reference type with mutable properties. A record is a C# 9+ reference type designed for immutable data: it generates value-based equality, a deconstructor, and a ToString that prints all members. For DTOs deserialized from JSON, records give you clean immutability and easy equality checks in tests. Use records when your target project is on .NET 6 or later and you do not need to mutate the object after binding.
Should I use System.Text.Json or Newtonsoft.Json attributes?
System.Text.Json is the default serializer in .NET Core 3.0 and later, is faster, and uses less memory. Its attribute is [JsonPropertyName("name")]. Newtonsoft.Json is the older library, still common in ASP.NET MVC projects, and its attribute is [JsonProperty("name")]. Pick the one your project already uses. If you are starting a new project on modern .NET, System.Text.Json is the right default.
Why are the generated property names in PascalCase?
C# convention is PascalCase for public properties. The tool converts incoming camelCase or snake_case JSON keys to PascalCase (for example userName becomes UserName, first_name becomes FirstName) and emits a [JsonPropertyName] attribute carrying the original key so deserialization still binds correctly. If a property name collides with a C# keyword, it is escaped with the @ prefix (for example @class, @event).
Does this tool send my JSON to a server?
No. Parsing and code generation run entirely in JavaScript inside your browser tab. The page makes no network request with your input and the JSON is discarded when the tab closes. You can disconnect from the internet after the page loads and the converter still works, which makes it safe to paste internal API responses or sample payloads.
How are arrays and null values handled?
An array of uniform objects produces a single element class shared by every item, and the property type becomes a List of that class. An empty array defaults to List of a placeholder. When nullable reference types are enabled, reference-type properties are emitted with a trailing ? unless the field appears with a non-null value in the sample. With nullable reference types off, the ? suffix is omitted.
Can I convert JSON to other languages with this tool?
Yes. The same input can be converted to TypeScript interfaces, Go structs, Rust structs, and Python dataclasses or TypedDicts using the sibling converters. If you need a typed model in more than one language, paste the same JSON into each converter so the property names and nesting stay consistent across your stack.
Related JSON and developer tools
Generate Go structs from JSON payloads
Produce serde-compatible Rust structs
Convert JSON into TypeScript interfaces
Generate Python dataclasses and type hints
Validate and pretty-print JSON with error details
Format and minify JSON for readable diffs