Code Generators
JSON to C# Converter
Paste a JSON or XML sample — the format is auto-detected — and get typed C# models with names, nesting and nullability inferred for you.
In short
Convert JSON to C# online: paste a sample and generate typed C# models with serialization helpers — powered by quicktype, in your browser.
By NaveenKumar T · Updated
- JSON or XML input, auto-detected
- Nested objects become types
- Nullability inferred
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
Example: JSON order payload to C#
A small order document with a nested object, an array of two items, a decimal total and a null field — enough to show how each C# type, list and nullable is inferred. The same page accepts the XML form of this order too, so the sample below is an example rather than the only accepted format. The right-hand side is the exact output of this tool.
{
"id": "ord_9F2KX",
"status": "processing",
"paid": true,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com", "phone": null },
"items": [
{ "sku": "KB-75", "qty": 1, "price": 149.99 },
{ "sku": "MS-12", "qty": 2, "price": 39.5 }
],
"total": 228.99
}// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var root = Root.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Root
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("paid")]
public bool Paid { get; set; }
[JsonProperty("customer")]
public Customer Customer { get; set; }
[JsonProperty("items")]
public Item[] Items { get; set; }
[JsonProperty("total")]
public double Total { get; set; }
}
public partial class Customer
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("phone")]
public object Phone { get; set; }
}
public partial class Item
{
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("qty")]
public long Qty { get; set; }
[JsonProperty("price")]
public double Price { get; set; }
}
public partial class Root
{
public static Root FromJson(string json) => JsonConvert.DeserializeObject<Root>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Root 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 }
},
};
}
}
Look for the "phone" field: sampled as null, it is typed as object. "items" became Item[], and "total" is double.
Learn more
From JSON sample to C# types
Hand-writing model classes from a JSON payload is transcription work: read a field, type a property, repeat eighty times, miss one. This page runs quicktype (the same engine behind the best-known generators) against your document, merging array elements to find the common shape and naming nested types from their keys.
The generated Converter.Settings sets DateParseHandling.None on purpose: with Newtonsoft's default, every ISO-8601 string in the payload would silently become a DateTime and serialize back in a different format. Keep that setting when you build your own JsonSerializerSettings, or convert dates deliberately with a JsonConverter on the fields that really are dates.
How JSON values become C# types
Inference works from the values in your sample. Strings map to string, integers and decimals are told apart (228.99 becomes double, while 1 and 2 stay integral), true/false become the language's boolean, and an object such as "customer" becomes its own named type. The "items" array is typed as Item[] because every element shares the same keys — quicktype unifies the shapes of all array elements into one Item type.
Nulls are the one place inference needs your help: "phone": null carries no type information, so the field is emitted as object. If the real API sometimes sends a string there, include a sample where the field is populated (paste several documents merged into one array) and the generated type becomes the union of what it saw.
Pasting XML works here too — a sample starting with "<" is parsed before inference runs — but if XML is your usual input, XML to C# is the page written for it: same generator, with the element-versus-attribute typing rules and a worked XML example.
Annotations, nulls and nested arrays in the generated C#
Every property carries a Newtonsoft [JsonProperty] attribute holding the wire name, and properties that were missing from one of several merged samples add NullValueHandling.Ignore so absent keys are not written back as null. Strings that hold a number or a boolean — the normal case for XML attributes — additionally get a [JsonConverter] pointing at a generated ParseStringConverter, so qty="1" lands in your model as a long. Classes are declared partial, ready to extend in a second file, and one internal Converter.Settings disables DateParseHandling and MetadataPropertyHandling.
A property whose only sampled value is null is typed object: you get the attribute and nothing else. Add a document where the field carries a value and it narrows to the real type, made nullable where C# needs it — long? or bool? — with NullValueHandling.Ignore attached when the key is sometimes absent. Reference types such as string are nullable already, so nothing is annotated; if your project enables nullable reference types, expect uninitialised-property warnings on exactly those properties, because quicktype cannot know which strings are truly required.
Each nested object becomes its own partial class named from the key — Customer, Item — used directly as the property's type. Arrays of objects are generated as C# arrays (Item[]), not List<Item>; change the declaration if your code wants list semantics, because Newtonsoft deserializes into either. Switching to System.Text.Json means swapping [JsonProperty] for [JsonPropertyName] and rewriting the converters, but the class tree carries over unchanged. An empty array degrades to object[] with no element class, so give every collection two representative elements.
Help
Frequently asked questions
Everything you need to know about the JSON to C#.
01How do I generate C# classes from JSON?
Paste a representative document — JSON or XML, the format is detected automatically — and press Convert. A real API response works best. Nested objects become their own C# types, arrays become Item[], and the result downloads as Root.cs.
02What does the generated C# code include?
C# output is property-based POCO classes with Newtonsoft.Json attributes and converter helpers — paste a payload, get the models your HttpClient code deserializes into.
03How do I use the generated C# code in my project?
Add the Newtonsoft.Json NuGet package, include the file, and call Root.FromJson(jsonString); the Serialize extension class provides root.ToJson() for the other direction. Properties carry [JsonProperty] attributes with the original wire names, and the shared Converter settings switch on strict date handling and metadata processing.
04Can I use System.Text.Json instead of Newtonsoft?
The classes are plain POCOs — only the attributes and helper target Newtonsoft. For System.Text.Json, swap [JsonProperty] for [JsonPropertyName]; the structure (the hard part) is already generated.
Keep working
Related tools
JSON to TypeScript
Code Generators
JSON to Java
Code Generators
JSON to Python
Code Generators
JSON Formatter
Format, beautify, and validate JSON instantly in a professional VS Code-style editor — free, fast, and 100% private.
.NET Escape
C#-ready string literals from any text — standard escapes plus \uXXXX Unicode, decoded back just as easily.
C# Formatter
Instant structural beautification for C# — paste any fragment, get readable indentation back.