How the JSON to C# Class works
The generator parses your JSON and records the kinds of values found at every position. All objects at the same
position, such as every item of an array, are merged into one class, so a sample with several items gives better
results than a single item. Nested objects become their own classes named after the property, and array item classes
use the singular form of the property name, so items produces an Item class.
Types are chosen as follows: string, bool, int for whole numbers that fit in
32 bits and long for larger ones (decided from the original digits, so large IDs are not rounded),
double or decimal for numbers with a fraction or exponent, DateTime for strings
that are all ISO 8601 dates, and List<T> for arrays. When a property holds different kinds of
values, or only null, its type is object. A value type becomes nullable, such as
int?, when the property is null in any sample or missing from some of the merged objects.
Property names are converted to PascalCase. With System.Text.Json or Newtonsoft.Json attributes selected, each
property also gets an attribute with the original JSON key, so user_name still maps to
UserName. When the top-level JSON is an array, the root class describes its items and a comment reminds
you to deserialize it as a list.
How to use the JSON to C# Class
- Paste a sample JSON object or array of objects into the input box, open a .json file, or select Load example.
- Set the root class name and an optional namespace, choose System.Text.Json, Newtonsoft.Json or no attributes, and pick the types for whole and decimal numbers.
- Select Generate C#, or press Ctrl + Enter (Cmd + Enter on a Mac).
- Copy the classes into your project or download them as a .cs file.
Example
This input:
{"id": 90071992547409931, "user_name": "ann", "createdAt": "2024-05-01T10:30:00Z",
"items": [{"sku": "KB-1", "qty": 1}, {"sku": "MS-2", "qty": null}]}
produces, with the default options:
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("user_name")]
public string UserName { get; set; }
[JsonPropertyName("createdAt")]
public DateTime CreatedAt { get; set; }
[JsonPropertyName("items")]
public List<Item> Items { get; set; }
}
public class Item
{
[JsonPropertyName("sku")]
public string Sku { get; set; }
[JsonPropertyName("qty")]
public int? Qty { get; set; }
}
Common use cases
- Creating DTOs for a third-party REST API from an example response.
- Generating request and response models for ASP.NET Core or ASP.NET Web API controllers.
- Building classes to deserialize JSON configuration or message queue payloads.
- Starting a strongly typed client for a webhook whose documentation only shows sample JSON.
Common errors and how to fix them
- The JSON must be an object, or an array of objects, to generate C# classes
- A bare string, number, or an array of primitives has no properties to turn into a class. Paste a sample object, or an array whose items are objects.
- A property was generated as object
- That field was null in every record you supplied, so there is no type information to infer from. Provide a sample where the field has a real value, or set the property type by hand afterwards.
- Deserialisation returns nulls at runtime
- Check the attribute option you generated with. Without attributes, your serialiser must be configured with a naming policy that matches the JSON keys; choosing the System.Text.Json or Newtonsoft option emits explicit attributes instead, which removes the guesswork.
- A number came out as the wrong type
- Types are inferred from the sample you provide, so a value of 1 is inferred as int even if the same field holds a decimal elsewhere. Supply a representative sample, or widen the type after generating.
Frequently asked questions
How are the property types chosen?
Strings become string, true and false become bool, whole numbers become int, or long when a value does not fit in 32 bits, and numbers with a fraction or exponent become double or decimal, as you choose. Strings that are all ISO 8601 dates become DateTime unless you turn that off, and arrays become List<T>. A property with mixed kinds of values, or only null, becomes object.
When is a property nullable?
A value type such as int, long, double, decimal, bool or DateTime gets a ? when the property is null in any sample or is missing from some of the objects that were merged, for example some items of an array. Reference types such as string, lists and classes are left as they are.
How are large IDs handled?
The number type is decided from the digits exactly as written in your JSON, so an ID such as 90071992547409931 is correctly detected as long instead of being rounded. Whole numbers too large even for long become decimal (up to 28 digits) or double.
Why use several items in a sample array?
All objects at the same position are merged into one class. The more items your sample has, the better the tool can tell which properties are optional, which can be null and which numbers need long or decimal.
How are class and property names created?
Keys are converted to PascalCase, so user_name becomes UserName. Nested classes are named after their property, and array item classes use the singular form, so items gives Item. Duplicate names get a number suffix. With attributes enabled, [JsonPropertyName] or [JsonProperty] keeps the mapping to the original key.
Is my JSON uploaded to your server?
No. The classes are generated in your browser, and the JSON you paste or open is not sent to our server.