JSON to C# Class Converter
Generate C# classes with properties and JsonProperty attributes from JSON, ready for Newtonsoft.Json.
Generate C# classes from JSON
Deserializing JSON in .NET starts with a model class whose properties match the payload. This converter writes those classes for you: camelCase keys become PascalCase properties with a [JsonProperty] attribute holding the original key, nested objects become their own partial classes, arrays become typed arrays, whole numbers become long and decimals double. It also adds a static FromJson method and a ToJson extension method with converter settings for Newtonsoft.Json (Json.NET).
How to use
- Paste a JSON sample, such as a response from the API your service calls.
- Enter the root class name, for example Product.
- Install the Newtonsoft.Json NuGet package, add the file to your project, rename the QuickType namespace to your own, and call Product.FromJson(json).
Frequently asked questions
- Does it work with System.Text.Json?
- The attributes and helpers target Newtonsoft.Json. For System.Text.Json, keep the properties, replace [JsonProperty] with [JsonPropertyName] from System.Text.Json.Serialization, and deserialize with JsonSerializer.Deserialize<Product>(json).
- Why are numbers generated as long and double?
- JSON numbers have no fixed size, so long and double avoid overflow surprises. Change them to int or decimal after copying if your domain needs it, for example decimal for money.
- Why are the classes declared as partial?
- Partial classes let the generated properties and the FromJson helper live in separate declarations, and let you add your own members in another file without editing the generated one.
Example: JSON to C#
Input JSON
{
"productId": 7,
"title": "Mechanical Keyboard",
"price": 129.99,
"inStock": true,
"tags": ["hardware", "usb-c"]
}Generated C# (Product)
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var product = Product.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Product
{
[JsonProperty("productId")]
public long ProductId { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("price")]
public double Price { get; set; }
[JsonProperty("inStock")]
public bool InStock { get; set; }
[JsonProperty("tags")]
public string[] Tags { get; set; }
}
public partial class Product
{
public static Product FromJson(string json) => JsonConvert.DeserializeObject<Product>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Product self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}