JSON to C# Classes: Generate DTOs from a Sample Payload

2026-07-22

Why convert JSON to C#

Third-party APIs often ship a JSON sample. Hand-writing C# models is slow and easy to miss fields. ComTools JSON to C# (Tools/Json/ToCSharp) parses JSON locally, infers types from values, and emits a public class with auto-properties as a DTO draft.

Type inference

JSON value C# type
Integer int
Decimal double
Boolean bool
String / null string
Primitive array List<int> / List<string> / …
Object / object in array Type named after the property, or List<object>

Property names are PascalCased (userIdUserId). Root class defaults to Root. If the root is an array, the first element drives the shape.

Steps

  1. Open JSON to C#.
  2. Paste a JSON sample (or use Sample).
  3. Optionally validate first.
  4. Click Run and copy the C# into your project.

Example

Input:

{
  "id": 1,
  "name": "Alice",
  "active": true,
  "score": 98.5,
  "tags": ["vip", "new"]
}

Output sketch:

public class Root
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public double Score { get; set; }
    public List<string> Tags { get; set; }
}

Common scenarios

Scenario Tip
Web API response DTOs Paste a real response → generate → add namespace/attributes
Nested objects Finish nested classes by hand, or generate from smaller samples
Empty array [] Yields an empty shell—use a sample with items
Readable JSON only Formatter
From SQL schema SQL to C#

Hand-tune after generation

  • Add namespace, nullable annotations, required
  • [JsonPropertyName] for System.Text.Json / Newtonsoft
  • Map date strings to DateTime / DateTimeOffset
  • Complete nested class definitions
  • Use long when values exceed int

Treat output as a starting point, not production-ready code.

Notes

  • Local browser processing only.
  • Standard JSON required—no trailing commas or comments.
  • Mixed types across array items follow the sample you paste—use representative data.

FAQ

Records / key attributes?
Classic class + { get; set; } only—upgrade manually.

Root is an array?
Uses the first element’s shape; pasting a single object is clearer.

vs Visual Studio “Paste JSON as Classes”?
Same idea; the web tool needs no IDE for a quick draft.


中文版