How it works
Paste a JSON sample and get C# types for System.Text.Json:
- Every object becomes a class or record named after its property (
customer→Customer,items→Item). The root isRootunless you rename it. - Property names are PascalCase, and the JSON key is kept in
[JsonPropertyName]. - Arrays of objects are merged, so keys that only some items have are still captured, as nullable properties.
- Numbers become
int,long,ulongordecimalbased on the values seen, ordoublewhen any value has a fraction. Arrays becomeList<T>, empty objectsDictionary<string, object?>, and unknown valuesobject?. usingdirectives are added only when needed; a file-scopednamespaceis added when you set one.
Example
{
"user_id": 42,
"name": "Linh",
"tags": ["admin"],
"addresses": [
{ "city": "Hanoi", "zip": "100000", "note": null },
{ "city": "HCMC", "zip": "700000", "primary": true, "note": "Gate 2" }
]
}
becomes
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("user_id")]
public int UserId { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } = "";
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = new();
[JsonPropertyName("addresses")]
public List<Address> Addresses { get; set; } = new();
}
public class Address
{
[JsonPropertyName("city")]
public string City { get; set; } = "";
[JsonPropertyName("zip")]
public string Zip { get; set; } = "";
[JsonPropertyName("note")]
public string? Note { get; set; }
[JsonPropertyName("primary")]
public bool? Primary { get; set; }
}
Options
- Root name: name of the top-level type.
- Style: Class { get; set; } or Record (positional).
- [JsonPropertyName] on all: annotate every property, or only the ones whose key differs beyond case.
- Nullable reference types: emit
string?and initializers for a project with<Nullable>enable</Nullable>. - Namespace (under More options): adds
namespace MyApp.Models;.
Reserved words and odd keys
C# keywords are all lower-case, so PascalCase names never need the @ prefix: class becomes Class and default becomes Default. Keys starting with a digit get a Field prefix (2fa → Field2fa), and keys with spaces or dashes are joined (first name → FirstName). A member that would have the same name as its class ({"address": {"address": "…"}}) is renamed AddressValue, because C# rejects that with error CS0542. Classes that would shadow List or Dictionary get a Type suffix.
Deserializing
using System.Text.Json;
var root = JsonSerializer.Deserialize<Root>(json)!;
// Root array: var items = JsonSerializer.Deserialize<List<RootItem>>(json)!;
When the JSON root is an array or a primitive, the output starts with a comment such as // JSON root type: List<RootItem>.
Common mistakes
- Generating from one object. Every key becomes required. Paste several real responses so optional keys become nullable.
- Whole-number money. A price of
25becomesintand a later25.5throws aJsonException. Usedecimalfor currency. object?properties. The sample value was null, an empty array or mixed types. System.Text.Json reads these asJsonElement; replace them with real types.
Need the same model elsewhere? See JSON to Java, JSON to Kotlin and JSON to TypeScript, or validate the payload first with the JSON Formatter.
Frequently asked questions
Why does every property have [JsonPropertyName]?
System.Text.Json matches property names case-sensitively by default, so `UserName` would not bind to a `userName` key. The attribute makes the mapping explicit and works with any `JsonSerializerOptions`. If you use `JsonSerializerDefaults.Web` (the ASP.NET Core default) or `PropertyNameCaseInsensitive = true`, turn the option off and only keys that differ beyond letter case (`user_id`, `first name`, `2fa`) keep the attribute.
What does the nullable reference types option change?
With it on, reference types that can be null or missing get a `?` (`string?`, `Address?`), and non-nullable ones get an initializer (`= "";` or `= new();`) so the compiler raises no CS8618 warnings. Value types such as `int?` and `bool?` are nullable in both modes.
Class or record?
Classes with `{ get; set; }` work everywhere and are easy to modify. Positional records are immutable and shorter; System.Text.Json deserializes them through the constructor, and the attribute is written as `[property: JsonPropertyName(...)]` so it lands on the generated property.
How are large integers typed?
From the sample range: `int` up to 2,147,483,647, then `long`, `ulong` for positive values up to 2^64−1, and `decimal` beyond that. Integers that do not fit in `decimal` produce a warning, because System.Text.Json cannot read them into any built-in numeric type.
Is my JSON uploaded?
No. The classes are generated entirely in your browser; nothing is sent to a server.
Last updated