Youtilities

Popular

All tools

JSON to C# Class

Generate C# classes for System.Text.Json or Newtonsoft from any JSON. Nullable types detected.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

C#
C# types appear here.

What this does

Paste JSON and get C# classes with auto-properties, annotated for System.Text.Json ([JsonPropertyName]) or Newtonsoft.Json ([JsonProperty]). Nested objects become their own classes; arrays of objects are merged into one class.

Types

Whole numbers are long, decimals double. A key that is missing from some items, or sometimes null, gets a nullable type such as string? or long?. Arrays are List<T>; empty arrays and mixed values are object.

Names

Properties are PascalCase and the original key is always kept in the attribute, so first-name maps to FirstName. Two objects with the same shape share one class.

Using the classes

With System.Text.Json, read the data with JsonSerializer.Deserialize<Root>(json); with Newtonsoft, JsonConvert.DeserializeObject<Root>(json). Every property carries its JSON name in an attribute, so the casing policy of the serializer does not matter and first-name still maps to FirstName. If the top level is an array, the output starts with a comment showing the List<...> type to deserialize into. Values of mixed type come through as object: System.Text.Json fills them with JsonElement and Newtonsoft with JToken.

Nullable warnings

Types such as string? need nullable reference types, which are on by default in new projects (<Nullable>enable</Nullable>). With them on, a non-nullable string or class property without a value gives warning CS8618. Fix it by adding = "" or = new(), by marking the property required (C# 11), or by making it nullable if the API can leave it out. Turning on All optional makes every property nullable at once.

long, double and money

Whole numbers become long so large ids fit; decimals become double. For prices and amounts, change double to decimal, which stores values like 0.1 exactly. System.Text.Json will not read a number sent as a string, such as "42", unless you set NumberHandling to AllowReadingFromString. A property that would share its class's name gets Value added to it, since C# forbids a member with the same name as its type.

All json tools