JSON to C# Class
Generate C# classes from JSON
About JSON to C# Class
JSON to C# Class writes C# model classes from a JSON sample with quicktype-core, inside your browser. The code sits in namespace App with the System.Text.Json usings; each nested object gets a public partial class, properties are PascalCase, and a [JsonPropertyName] attribute keeps every original key. Whole numbers are long, decimals double, and arrays become arrays such as Item[]. Properties missing from some array elements are nullable and carry a [JsonIgnore] attribute for null values. It targets System.Text.Json only, with no Newtonsoft attributes and no records. A value with two types, such as a number in one item and text in another, adds a union struct and the JsonConverter classes it needs.
Example
An order with line items; note appears in only one item, so it becomes nullable.
JSON input
{"orderId": 1001, "total": 49.90, "paid": true,
"items": [{"sku": "A1", "qty": 2}, {"sku": "B7", "qty": 1, "note": "gift"}]}C# output
namespace App
{
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Globalization;
public partial class Root
{
[JsonPropertyName("orderId")]
public long OrderId { get; set; }
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("paid")]
public bool Paid { get; set; }
[JsonPropertyName("items")]
public Item[] Items { get; set; }
}
public partial class Item
{
[JsonPropertyName("sku")]
public string Sku { get; set; }
[JsonPropertyName("qty")]
public long Qty { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("note")]
public string? Note { get; set; }
}
}How to generate C# classes from JSON
- 1Paste a JSON sample into "JSON input".
- 2Click "Convert" to get the classes in "C# output".
- 3Replace the namespace App line with the namespace of your project.
- 4Click "Copy" or "Download" to keep the code as "Model.cs".
Formats and limits
- Input
- JSON object or array of objects, as text
- Output
- C# classes in namespace App, saved as Model.cs
- Serializer
- System.Text.Json: [JsonPropertyName] on every property, [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] on optional ones
- Types
- long, double, bool, string, and arrays (T[]); object for a key that is null in every sample; a union struct when a value has two types
Where your JSON is processed
Classes are generated by JavaScript running on your own device; the JSON you paste and the C# you get are never uploaded. The quicktype library loads when you first press Convert. When the page displays an error, our error log receives its text, cleaned of quoted fragments, long numbers, and web or email addresses, with the tool's name and your browser's name and version.
Frequently asked questions
Does it work with Newtonsoft.Json (Json.NET)?
No. The generated attributes belong to System.Text.Json: [JsonPropertyName] for names and [JsonIgnore] for optional values. To use the classes with Newtonsoft.Json, replace them with its own [JsonProperty] attribute by hand.
Will the classes deserialize my JSON as they are?
Yes, when every key keeps one type: JsonSerializer.Deserialize maps each property to its original key through [JsonPropertyName]. When a value mixes types, pass Converter.Settings, which the output then contains, as the second argument. The types come only from your sample, so check them against more data.
Is a property typed object when its value is always null?
Yes. When a key is null in every element of your sample, no type can be inferred, so it becomes object. Paste an example where the key holds a real value, or set the type yourself, for example string? for an optional text.
Can I use int instead of long?
Yes. Whole numbers are always typed long so that large values fit; if your values are small, change long to int by hand, and the attribute and the key mapping keep working.
Tips and tricks
- ✓Paste an array with several items so that properties which are sometimes absent become nullable.