toolqik

140 · JSON

JSON to C#

Generate C# classes or records with System.Text.Json or Newtonsoft attributes.

Runs in your browser
Style
Attributes
Options
C# code

  

About this tool

To deserialize JSON in .NET you need a class per object with a property per key. Paste a JSON example and this tool writes them: PascalCase properties with attributes that map to the original JSON names, List<T> for arrays, nullable reference types for values that can be null, and required for fields that are always present.

Options

  • class generates public required string Name { get; set; } properties. required (C# 11 / .NET 7+) makes the compiler insist they are set, and System.Text.Json in .NET 8 enforces it when deserializing.
  • record generates positional records such as public record Order(string Id, double Total);, which are immutable and compare by value.
  • System.Text.Json adds [JsonPropertyName("user_name")], Newtonsoft adds [JsonProperty("user_name")]. Pick None if you configure a naming policy instead (for example JsonNamingPolicy.SnakeCaseLower in .NET 8).

Types

Whole numbers become int, or long when a value exceeds 32 bits; decimals become double (switch to decimal for money); true/false become bool. Fields that are nullable or missing from some samples get ?, which assumes nullable reference types are enabled (the default in new .NET projects). Mixed values become object; consider JsonElement to inspect them later.

How types are inferred

  • Every object becomes its own named type. Names come from the key (address → Address), and items of an array get the singular (orders → Order).
  • For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes null becomes nullable.
  • Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
  • Identical nested shapes are reused instead of generating duplicates.

The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.

Deserialize

// System.Text.Json
var root = JsonSerializer.Deserialize<Root>(json);
var items = JsonSerializer.Deserialize<List<Item>>(json);   // top-level array

// Newtonsoft.Json
var root = JsonConvert.DeserializeObject<Root>(json);

Visual Studio can also do this with Edit → Paste Special → Paste JSON as Classes; this tool adds attributes, records, nullability and required.

FAQ

How do I convert JSON to a C# class?

Paste the JSON above. A class (or record) is generated for each object, with attributes mapping properties to the original JSON keys.

Should I use System.Text.Json or Newtonsoft attributes?

Use System.Text.Json for new .NET projects; it is built in and faster. Use Newtonsoft if your project already depends on Json.NET.

What does the required keyword do?

In C# 11 and later, required properties must be set when the object is created. System.Text.Json in .NET 8 throws if a required property is missing from the JSON.

Why do some properties have a question mark?

They were null or missing in the sample, so they are nullable (string?, int?). This relies on nullable reference types, which are on by default in new projects.

Can I generate records instead of classes?

Yes. Choose record for positional records, which are immutable and support value equality.

esc
↑↓ navigate↵ open