Csharp Class from JSON

Convert JSON to C# class - records or classes, JsonPropertyName (System.Text.Json or Newtonsoft.Json), init-only setters, required keyword, mixed-type detection. Free.

Generate C# classes / records from JSON. Smart type inference (long vs int, ISO 8601 → DateTime, mixed-type arrays → List<object> with warning), JsonPropertyName attributes (System.Text.Json or Newtonsoft.Json), init-only setters, required keyword.

0 characters

Generated C# code


        

How to Use Csharp Class from JSON

  1. Paste your JSON object. Live JSON validity check shows ✓ or specific parse error.
  2. Enter class name and (optional) namespace. Pick type kind (class or record), JSON attribute style, array style (List<T> or T[]).
  3. Toggle options: PascalCase properties, nullable reference types, init-only setters (C# 9+), required keyword (C# 11+).
  4. Click Generate (or Ctrl+Enter). The generated code appears below with syntax highlighting.
  5. Copy or download as {ClassName}.cs. Drop into your .NET project - most options work in .NET 6 / 7 / 8.

Frequently Asked Questions

What’s the difference between class and record?

Class is mutable by default (with { get; set; }) and uses reference equality. Record (C# 9+) is for DTOs – value-equality is generated automatically, comparing by property values rather than reference. For JSON DTOs that you serialize/deserialize without modifying, records are usually the better choice. Records with init-only setters ({ get; init; }) are essentially immutable.

System.Text.Json vs Newtonsoft.Json – which?

System.Text.Json ships with .NET Core 3.0+ (no package install needed), is significantly faster, and is the modern recommendation. Newtonsoft.Json is the older standard, has more features (e.g., JsonExtensionData, custom contract resolvers), and is still widely used. The tool generates the correct attribute style for each: [JsonPropertyName] vs [JsonProperty]. They’re not interchangeable – pick to match your project’s reference.

What’s the “required” keyword for?

C# 11 added required to force properties to be initialized at construction. public required string Name { get; set; } means: if you write new Foo() without setting Name, the compiler errors. Useful for DTOs where every property is mandatory. Note: doesn’t conflict with nullable annotation – required string? means “must be set, but can be null”.

How are numbers typed?

int for integers up to 2^31-1 (2,147,483,647). long for integers between 2^31 and 2^53 (the limit of JavaScript’s Number precision). decimal for integers beyond 2^53 (rare). double for any non-integer (1.5, 99.99, etc.). For monetary values, you may want to manually change double to decimal in the output – JSON has no money type, so the tool can’t auto-detect.

How are dates typed?

Strings matching ISO 8601 format (2026-06-03T12:34:56Z, 2026-06-03) get typed as DateTime. For other date formats (Unix timestamps, RFC 2822) they stay as string – convert manually after deserialization. Note: DateTime doesn’t carry timezone information; for that, use DateTimeOffset (you’ll need to find/replace).

What about mixed-type arrays?

If your JSON has "data": [1, "hello", true], the elements have different types. The tool detects this and generates List<object> with a comment: // Note: source array had mixed element types. You’ll need to deserialize as JsonElement or dynamic and switch on the actual types at runtime.

What about reserved C# keywords as property names?

JSON keys like class, namespace, return are reserved in C#. The tool prefixes them with @: public string @class { get; set; }. This is valid C# syntax. Or, combine with JsonPropertyName attribute mode to keep the JSON key and rename the C# property: [JsonPropertyName("class")] public string ClassName { get; set; }.

What’s in the .cs file?

Plain C# source with using directives at top, optional namespace declaration, and one or more classes (one for the root + one per nested object). Indented per code-style conventions (4 spaces). Drop into Visual Studio, Rider, or VS Code with C# Dev Kit.

Is anything uploaded?

No. The parsing happens entirely in your browser – nothing is sent to a server, logged, or stored, and the tool keeps working offline once the page has loaded.