JSON → C# Class

Class:
JSON Input
Loading editor…
C# Classes
Loading editor…

JSON to C# Class Generator

Paste any JSON and get C# model classes instantly. Nested objects become separate classes, arrays become List<T>, and ISO dates become DateTime. Choose classic classes or C# 9 records, add System.Text.Json or Newtonsoft.Json attributes, and turn nullable reference types on or off to match your project.

Nested classes
Deep objects become separate named classes.
Class or record
{ get; set; } properties or positional records.
Attributes
[JsonPropertyName] or [JsonProperty] per field.
Client-side
No data is uploaded. Everything runs in your browser.

How JSON Maps to C# Classes

Every JSON object becomes a C# class with one property per key. Nested objects generate additional classes named after the key, and arrays of objects generate a class for the element type, with the key name singularized and wrapped in List<T>.

Input JSON:

{
  "id": 1,
  "name": "Alice",
  "created_at": "2024-05-01T10:30:00Z",
  "address": { "city": "Springfield", "zip_code": "12345" },
  "posts": [
    { "id": 101, "title": "Hello World" }
  ],
  "avatar": null
}

Generated C# (System.Text.Json, nullable on):

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

public class Root
{
    [JsonPropertyName("id")]
    public int Id { get; set; }

    [JsonPropertyName("name")]
    public string Name { get; set; } = string.Empty;

    [JsonPropertyName("created_at")]
    public DateTime CreatedAt { get; set; }

    [JsonPropertyName("address")]
    public Address Address { get; set; } = new();

    [JsonPropertyName("posts")]
    public List<Post> Posts { get; set; } = new();

    [JsonPropertyName("avatar")]
    public object? Avatar { get; set; }
}

Address and Post are generated as their own classes below Root. Deserialize with JsonSerializer.Deserialize<Root>(json), or JsonConvert.DeserializeObject<Root>(json) if you chose Newtonsoft.

Type Inference and Naming

JSON keys become idiomatic PascalCase properties — zip_code becomes ZipCode, user-id becomes UserId. A property that would share its class's name gets a Value suffix, since C# doesn't allow a member named like its enclosing type.

Whole numbers that fit in 32 bits become int, larger ones long, and decimals double. Strings in ISO 8601 format (2024-05-01T10:30:00Z) become DateTime; all other strings stay string. Booleans become bool.

A field that is null or missing in any sample is made nullable: int?, DateTime?, and — with nullable reference types on — string? or Address?. Numbers widen across samples — int and long become long, and any mix with decimals becomes double. A field that's only ever null, or whose values have incompatible types, falls back to object.

Every element of an array is merged into one class, including objects nested inside them, so a field that appears in only some elements is still generated (as nullable). If the same key holds differently shaped objects in different places, each gets its own class (Data, BData); identical shapes share one.

System.Text.Json vs Newtonsoft Attributes

System.Text.Json is built into .NET Core 3.0+ and is the default in ASP.NET Core. Choosing it adds [JsonPropertyName("original_key")] to each property.

Newtonsoft.Json (Json.NET) is still common in older projects and libraries. Choosing it adds [JsonProperty("original_key")] instead.

Both map the PascalCase property back to the exact JSON key, which matters for snake_case or kebab-case APIs. With neither selected, you rely on the serializer's naming policy instead — ASP.NET Core's web defaults match camelCase keys case-insensitively, but not snake_case.

Classes vs Records

Class generates mutable classes with { get; set; } properties — works on every .NET version and with every serializer.

Record generates positional records (C# 9 / .NET 5+), such as public record Post(int Id, string Title);. Records are immutable, compact, and compare by value — a good fit for DTOs and API responses. Attributes are emitted with the property: target ([property: JsonPropertyName("id")]) so they apply to the generated properties, which is what the serializers read.

Open-Source npm Package and CLI

The generator behind this page is open source: json-to-csharp on GitHub, published on npm under the MIT license. Use it to generate C# models in build scripts, code generators or CI.

From the command line:

npx json-to-csharp response.json --name User --attributes stj
curl -s https://api.example.com/user | npx json-to-csharp -n User --record

From JavaScript or TypeScript:

import { jsonToCSharp } from "json-to-csharp";

const cs = jsonToCSharp(json, { rootName: "User", style: "record", attributes: "newtonsoft" });

It takes the same options as this page — class or record, System.Text.Json or Newtonsoft attributes, nullable reference types — plus a namespace option for a file-scoped namespace.

Frequently Asked Questions

How do I deserialize JSON into the generated C# class?

With System.Text.Json: var root = JsonSerializer.Deserialize<Root>(json);. With Newtonsoft.Json: var root = JsonConvert.DeserializeObject<Root>(json);. If your JSON root is an array, deserialize into List<Root> instead.

Should I use System.Text.Json or Newtonsoft attributes?

Use System.Text.Json for new .NET projects — it's built in and faster. Use Newtonsoft if your project already depends on Json.NET or needs features it supports better, like complex polymorphism or JSON.NET-specific converters.

What does the Nullable option change?

It assumes nullable reference types are enabled (the default in .NET 6+ projects). Reference types that can be null or missing become string?, Address?, etc., and required ones get an initializer (= string.Empty; or = new();) so the compiler doesn't warn about CS8618. Turn it off for older projects; value types like int? stay nullable either way.

Why is a date string typed as DateTime?

Strings in ISO 8601 format (like 2024-05-01T10:30:00Z) are detected as dates, since both System.Text.Json and Newtonsoft parse them into DateTime automatically. Change the type to string or DateTimeOffset if you prefer.

Should I generate classes or records?

Records (C# 9+) are concise, immutable and compare by value — great for DTOs and API responses. Classes with { get; set; } are mutable and work on every .NET version, which suits older codebases or models you modify after deserializing.

Can I generate C# classes from JSON in a script or CI?

Yes. This converter is available as the open-source json-to-csharp package on npm. Run npx json-to-csharp data.json --name User from the command line, or import { jsonToCSharp } from "json-to-csharp" in Node.js — same options and output as this page.

Is my JSON uploaded anywhere?

No. The entire conversion runs in your browser with no network requests. Nothing is transmitted.