JSON to C# Class Generator

Paste JSON and get C# classes with [JsonPropertyName] attributes, ready for System.Text.Json. Runs locally in your browser.

Convert JSON to C# classes

Paste a JSON sample, for example an API response, and click Generate. This JSON to C# class generator turns every JSON object into a C# class with auto-properties. Property names are converted to PascalCase, and the original key is kept in a [JsonPropertyName] attribute, so System.Text.Json deserializes your data without a custom naming policy. The output starts with the two using directives it needs and #nullable enable, and targets .NET 6, 7 and 8.

How to use it

  1. Paste a JSON object, or an array of objects, into Input. Pick a sample where optional fields are present.
  2. Click Generate. Invalid JSON shows the parser error instead of code.
  3. Click Copy and paste into a new .cs file. Add a namespace line and rename Root to your model name.
  4. Deserialize with JsonSerializer.Deserialize<Root>(json).

Type mapping

JSON valueC# type
42long
9.5, or a mix of 2 and 2.5 across itemsdouble
"text"string, initialised to string.Empty
truebool
{ ... }A new class named after the key, initialised with default!
[ ... ]List<T>, initialised to new()
null next to a real valueNullable type such as double?
null everywhereobject?
Key missing from some array itemsNullable type such as string?

Example 1: an order with a nested customer

{"order_id":1042,"customer":{"name":"Ada","is_vip":true},"total":99.9,
 "lines":[{"sku":"A1","qty":2}],"coupon":null}

generates:

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

#nullable enable

public class Root
{
    [JsonPropertyName("order_id")]
    public long OrderId { get; set; }

    [JsonPropertyName("customer")]
    public Customer Customer { get; set; } = default!;

    [JsonPropertyName("total")]
    public double Total { get; set; }

    [JsonPropertyName("lines")]
    public List<Line> Lines { get; set; } = new();

    [JsonPropertyName("coupon")]
    public object? Coupon { get; set; }
}

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

    [JsonPropertyName("is_vip")]
    public bool IsVip { get; set; }
}

public class Line
{
    [JsonPropertyName("sku")]
    public string Sku { get; set; } = string.Empty;

    [JsonPropertyName("qty")]
    public long Qty { get; set; }
}

lines becomes List<Line> because array classes take the singular of the key. coupon is object? because the sample only shows null; change it to string? or a Coupon class once you know its real shape.

Example 2: optional and nullable fields

{ "items": [
  { "id": 1, "note": "a", "score": null },
  { "id": 2, "score": 3.5, "created_at": "2026-01-01" } ] }

generates a Root class with List<Item> Items and this Item class. note and created_at are missing from one item, so they become nullable, and score is nullable because one item has null:

public class Item
{
    [JsonPropertyName("id")]
    public long Id { get; set; }

    [JsonPropertyName("note")]
    public string? Note { get; set; }

    [JsonPropertyName("score")]
    public double? Score { get; set; }

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

Example 3: a top-level array

When the JSON starts with [, the generator describes the elements and adds a comment saying so:

// The top-level value is an array; the classes below describe its elements.

public class Root { ... }

// usage
var rows = JsonSerializer.Deserialize<List<Root>>(json);

Naming rules and edge cases

Related generators

If your source data is XML or YAML, use XML to C# or YAML to C#. The same JSON can also be turned into Java classes, TypeScript interfaces or Go structs, and all languages are available in the class generator hub. To tidy hand-edited C# afterwards, use the C# formatter, and to validate payloads at run time against a contract, generate a schema with the JSON Schema generator.

Frequently asked questions

Does this work with Newtonsoft.Json?

Yes, after one change: replace [JsonPropertyName("x")] with [JsonProperty("x")] and using System.Text.Json.Serialization; with using Newtonsoft.Json;. The class shapes are identical.

Can I get records instead of classes?

The generator emits classes with setters because they work with every serializer setting. To use records, change public class Item to public record Item; the properties stay valid.

Is Visual Studio's Paste JSON as Classes the same?

It is similar, but Visual Studio does not add [JsonPropertyName] attributes, so snake_case keys need a naming policy. This tool also works without an IDE.

Why is a property typed object?

The value was null in every place it appeared, so there was nothing to infer a type from. Add a real value to the sample or set the type by hand.

Is my JSON uploaded?

No. The JSON is parsed and the C# is generated in your browser; nothing is sent to a server.