Skip to content

JSON to C# Class Generator

Convert JSON to C# classes with a [JsonPropertyName] or [JsonProperty] attribute on every property, ready for nullable reference types.

Input · JSON
Output · C#
Valid JSONRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

JSON to C# classes for an order

{
  "orderId": 981,
  "placedAt": "2026-11-02T16:05:00Z",
  "customer": {
    "id": "C-311",
    "fullName": "Kofi Mensah",
    "loyaltyTier": "gold"
  },
  "lines": [
    {
      "productId": "P-9",
      "description": "Running shoes",
      "quantity": 1,
      "unitPrice": 119.99
    },
    {
      "productId": "P-12",
      "description": "Socks (3-pack)",
      "quantity": 2,
      "unitPrice": 12.5,
      "discount": 0.1
    }
  ],
  "paid": true
}

With Root name set to Order, the root class comes out as:

using System.Text.Json.Serialization;

public class Order
{
    [JsonPropertyName("orderId")]
    public long OrderId { get; set; }

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

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

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

    [JsonPropertyName("paid")]
    public bool Paid { get; set; }
}

and the customer as:

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

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

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

The lines array also produces a Line class. Its discount key is only on the second line, so it becomes double?: array items are merged first, and a key that is missing or null anywhere turns nullable (full rules on JSON to TypeScript).

Choices the generator makes

  • Numbers: long for whole numbers, double for decimals.
  • Arrays: List<T>, with the item class named in the singular (lines gives List<Line>).
  • Initializers: new projects enable nullable reference types, which warn (CS8618) when a non-nullable property could be null after construction. Strings therefore start as string.Empty, objects and lists as new().
  • Nullable values: ?, as in double? or Customer?.
  • Names: PascalCase, with the exact JSON key in the attribute. A member can’t share its class’s name (CS0542), so such a property gets Value appended.
  • Mixed or null-only values: object.
  • Dates: placedAt stays a string. Both serializers read ISO 8601 text like 2026-11-02T16:05:00Z into a DateTimeOffset if you change the type.

System.Text.Json or Newtonsoft.Json

Style only swaps the attribute and its using: [JsonPropertyName("orderId")] from System.Text.Json.Serialization, or [JsonProperty("orderId")] from Newtonsoft.Json. System.Text.Json matches names case-sensitively by default, so without the attribute orderId never fills OrderId. Newtonsoft.Json ignores case but still needs it for keys like loyalty_tier. ASP.NET Core’s web defaults ignore case too.

Deserialize the JSON

using System.Text.Json;

string json = File.ReadAllText("order.json");
Order order = JsonSerializer.Deserialize<Order>(json)!;
Console.WriteLine($"{order.Customer.FullName}: {order.Lines.Count} lines");

Deserialize returns null only for the literal JSON null, hence the !. In Newtonsoft.Json the call is JsonConvert.DeserializeObject<Order>(json). List<T> needs System.Collections.Generic, which SDK-style projects import implicitly. For other languages, the JSON Schema generator describes the same payload and JSON to Java builds the Jackson version.

Questions

Why long instead of int?

A sample cannot show how large a value will get. IDs and millisecond timestamps overflow int, whose maximum is 2,147,483,647, so long is the safe default. Change it where you know the range is small.

How do I leave null properties out when serializing?

In System.Text.Json, set DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull on JsonSerializerOptions. In Newtonsoft.Json, set NullValueHandling = NullValueHandling.Ignore on JsonSerializerSettings.

Does the output compile on .NET Framework?

Not as is. The classes use target-typed new() from C# 9 and nullable annotations from C# 8, while .NET Framework projects default to C# 7.3. Raise LangVersion in the project file, or remove the initializers and the question marks on reference types.

Related tools

Esc