JSON to C# Class
Convert JSON to C# classes or records for System.Text.Json or Newtonsoft.Json, with nullable types, JsonPropertyName attributes and DateTimeOffset/Guid detection.
For strings that look like dates and UUIDs
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("username")]
public string Username { get; set; } = string.Empty;
[JsonPropertyName("email")]
public string Email { get; set; } = string.Empty;
[JsonPropertyName("isActive")]
public bool IsActive { get; set; }
[JsonPropertyName("rating")]
public double Rating { get; set; }
[JsonPropertyName("createdAt")]
public DateTimeOffset CreatedAt { get; set; }
[JsonPropertyName("avatarUrl")]
public object? AvatarUrl { get; set; }
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = new();
[JsonPropertyName("address")]
public Address Address { get; set; } = new();
[JsonPropertyName("orders")]
public List<Order> Orders { get; set; } = new();
}
public class Address
{
[JsonPropertyName("street")]
public string Street { get; set; } = string.Empty;
[JsonPropertyName("city")]
public string City { get; set; } = string.Empty;
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; } = string.Empty;
}
public class Order
{
[JsonPropertyName("orderId")]
public string OrderId { get; set; } = string.Empty;
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("items")]
public int Items { get; set; }
[JsonPropertyName("shipped")]
public bool Shipped { get; set; }
[JsonPropertyName("coupon")]
public string? Coupon { get; set; }
}
Arrays of objects are merged: a key missing from some items becomes optional, null makes a field nullable, and identical shapes share one type. Paste several examples in an array for the most accurate types.
About this tool
Paste JSON and get C# classes or records ready for System.Text.Json or Newtonsoft.Json. Properties are PascalCase with [JsonPropertyName] or [JsonProperty] attributes, so every key maps back exactly. Nullable reference types are used correctly: optional and null values become nullable, and non-null strings, lists and objects get initial values so the compiler raises no CS8618 warnings. Integers become int or long depending on the values, decimals become double, and strings that look like timestamps, dates or UUIDs can become DateTimeOffset, DateOnly or Guid. Nested objects get their own classes, lists become List<T>, and ID-keyed objects become Dictionary<string, T>. Choose a file-scoped namespace and switch to Go, Rust, Python or Kotlin from the same page.
How to convert JSON to C# classes
Generate C# types from a JSON sample.
- 1
Paste JSON
Paste a JSON object, or an array of examples.
- 2
Choose options
Pick System.Text.Json or Newtonsoft, class or record, and a namespace.
- 3
Copy the code
Copy the classes or download Types.cs.