Format Code Now
/

JSON to C# Class Converter

FV-14

Paste a JSON sample and press Convert to get C# classes with properties, List<T> collections and a JSON attribute on every property whose name differs from its key. It runs in your browser.

Runs in your browser. Nothing is uploaded.

JSON

C#

How to use it

  1. Paste a typical JSON response into the input box, or open a .json file.
  2. Pick System.Text.Json or Newtonsoft.Json for the attributes, and turn nullable reference types on or off.
  3. Press Convert and copy the classes into your project, or download them as a .cs file.
  4. Deserialize with JsonSerializer.Deserialize<Root>(json) or JsonConvert.DeserializeObject<Root>(json).

How the classes are built

Every object in the sample becomes a class, and nested objects become their own classes named after the key. Properties are written in PascalCase with { get; set; }. When a property name is not an exact copy of the JSON key, an attribute carries the original key: [JsonPropertyName] for System.Text.Json or [JsonProperty] for Newtonsoft.Json. Arrays become List<T>, and the item type is worked out from every item in the array, not just the first.

Numbers, nulls and missing keys

Whole numbers become int, or long once they pass the 32-bit range, and anything with a decimal point or exponent becomes double. A whole number beyond long also becomes double and can lose digits, so the line gets a comment. A key that is null in some items, or missing from some objects, gets a nullable type such as int? or, with nullable reference types on, string?. A key that is always null, mixed types and empty arrays become object.

What a sample cannot tell it

The converter only sees the sample you paste. A key that never appears in the sample has no property, and a field that is always a whole number in the sample becomes int even if the API can send decimals. Dates stay string because JSON has no date type; change them to DateTime or DateTimeOffset by hand. Check the result against the API documentation before you rely on it.

Questions people ask

How do I convert JSON to a C# class?

Paste the JSON, press Convert, and copy the generated classes into your project. Then call JsonSerializer.Deserialize<Root>(json) for System.Text.Json or JsonConvert.DeserializeObject<Root>(json) for Newtonsoft.Json. The class named Root matches the top-level object.

Should I choose System.Text.Json or Newtonsoft.Json?

Choose the library your project already references. The only difference in the output is the attribute: [JsonPropertyName("key")] for System.Text.Json and [JsonProperty("key")] for Newtonsoft.Json. The class and property shapes are the same.

What does the nullable reference types option change?

With it on, the file starts with #nullable enable, nullable strings and classes are written as string? and Address?, and other reference properties get = default!; so the compiler does not warn about uninitialized properties. Value types such as int use int? either way when a key can be null.

What happens when the JSON starts with an array?

The items become a class named after the item, for example RootItem, and a comment at the top says the document is a List of that class. Deserialize into List<RootItem>.

Why is a very large number written as double?

A whole number beyond the range of long has no exact C# integer type among the simple choices here, so it becomes double, which can round it. The generated line has a comment. If you need every digit, change the property to decimal or a string by hand.

Does it detect dates and GUIDs?

No. Strings stay string, even when they look like an ISO date or a UUID. Change those properties to DateTime, DateTimeOffset or Guid yourself if you want the typed values.

About this tool

Infers classes from the sample you paste, so it only knows what that sample shows. Every object becomes a class with { get; set; } properties in PascalCase, and an attribute keeps the original key whenever the property name is not an exact copy of it. Whole numbers become int, or long beyond the 32-bit range; anything with a decimal point or exponent becomes double. A whole number beyond long also becomes double, which can round it, and a comment marks the line. Arrays become List<T>. A key that is null in some items or missing from some objects gets a nullable type: int? for value types, and string? when nullable reference types are on. With that option on, other reference properties get = default!; so the compiler stays quiet. A key that is null in every item, mixed types and empty arrays become object. Strings stay string, so dates are not turned into DateTime. When the top level is an array, the class is named after its items and a comment says so.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.