JSON to C# Class & Record Generator

Need to convert C# into JSON Schema?

Convert DTOs and records into shareable schemas.

C# Class → JSON Schema

JSON Input

Loading editor…

Generated C#

Configuration

Enter JSON data to generate strongly-typed C# classes and records for your .NET projects

Classes, records, DTOs, and properties with nullable reference types

How to convert JSON to C# – step-by-step guide

Use this JSON to C# generator to create C# classes or records from JSON samples for ASP.NET Core APIs and .NET apps.

  1. Step 1 – Paste a JSON sample

    • Paste a representative JSON object or array into the left editor.
    • Include nested objects, arrays, and nullable fields so types are inferred correctly.
    • Use Import to load JSON from a file, URL, or sample data.
  2. Step 2 – Choose C# class options

    • Set Class Name and Namespace to match your project structure.
    • Choose a JSON framework (System.Text.Json or Newtonsoft.Json) if you want attributes.
    • Enable nullable reference types so optional fields map to string? and similar types.
  3. Step 3 – Review the generated code

    • Check field names, types, and how arrays/objects are modeled.
    • Adjust options like Root Type Name, null handling, and frameworks if available.
    • If a field is inferred incorrectly, tweak your sample JSON and regenerate.
  4. Step 4 – Use the generated DTOs in .NET

    • Paste the output into your project and add it to your API/DTO folder.
    • Deserialize JSON into the root type using your chosen serializer.
    • Keep DTOs separate from domain models when you need versioning and backward compatibility.
  5. Step 5 – Copy or download

    • Copy the output into your project or download it as a file.
    • Run your formatter/linter to match your code style.
    • Add JSON parsing/serialization libraries if your language requires them.

Quick tips

  • Prefer records for immutable models when possible.
  • Use [JsonPropertyName]/[JsonProperty] when JSON keys don’t match C# naming.
  • Validate input early to avoid partially-initialized objects.
Example output (simplified)
// JSON input
{
  "id": 123,
  "name": "Maeve Winters",
  "email": "maeve@example.com",
  "active": true,
  "roles": ["admin", "editor"],
  "metadata": { "plan": "pro" },
  "createdAt": "2024-03-01T10:15:00Z",
  "score": 99.5,
  "notes": null
}

// Generated C# DTOs (simplified)
public class Metadata
{
    public string Plan { get; set; } = string.Empty;
}

public class Root
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string? Email { get; set; }
    public bool Active { get; set; }
    public List<string> Roles { get; set; } = new();
    public Metadata Metadata { get; set; } = new();
    public DateTime CreatedAt { get; set; }
    public double Score { get; set; }
    public object? Notes { get; set; }
}

Related JSON & C# tools

Explore more JSON and schema tools that work great alongside this JSON to C# generator.

Frequently Asked Questions

What C# features are supported in this JSON to C# generator?

The generator creates C# classes and C# 9+ records with properties, supports JSON.NET and System.Text.Json attributes, and follows modern .NET naming conventions with proper namespaces.

Can I generate C# records and DTO models instead of only classes?

Yes! Select 'Record' as the framework to generate modern C# records with immutable properties that are ideal for data transfer objects (DTOs), API contracts, and value types.

How does JSON serialization work with the generated C# types?

The generator supports both Newtonsoft.Json and System.Text.Json attributes for proper JSON serialization and deserialization with custom property names, null handling, and flexible configuration.

What about nullable reference types and optional JSON fields?

The generator includes nullable annotations (object?) for properties that can be null, following modern C# nullable reference types conventions and making it easier to map optional JSON fields safely.

Can I use the generated C# classes in ASP.NET Core or Blazor projects?

Yes. The JSON to C# code works well in ASP.NET Core controllers, minimal APIs, Blazor components, and any .NET application that needs strongly-typed models for JSON payloads.

Is this JSON to C# generator free to use?

Yes, the JSON Swiss JSON to C# generator is free to use in the browser. You can paste JSON, configure options, and generate C# code without installing any tools or extensions.

JSON to C# Class & Record Generator Online | JSONSwiss