JSON to C# Records
Turn a JSON file into C# records and unions in one click. Types and nullability are worked out from
the data itself, and the result is ready for .NET 11 System.Text.Json with JsonSerializerOptions.Web.
Usage
Right-click a .json file in the Explorer or in the editor and choose Generate C# Records (Types.cs).
It is also in the Command Palette as JSON to C#: Generate C# Records (Types.cs).
Types.cs is written next to the JSON file. If it already exists, you are asked before it is overwritten.
Then read the file with:
var root = JsonSerializer.Deserialize<Root>(json, JsonSerializerOptions.Web);
Trailing commas in objects and arrays are supported. To deserialize a file containing them, enable
AllowTrailingCommas:
var options = new JsonSerializerOptions(JsonSerializerOptions.Web) { AllowTrailingCommas = true };
var root = JsonSerializer.Deserialize<Root>(json, options);
Example
This JSON:
{
"customers": [
{ "id": "C001", "name": "Ada" }
],
"orders": [
{
"id": "O1001",
"customerId": "C001",
"placedAt": "2026-01-02T10:00:00Z",
"items": [{ "productId": "P100", "quantity": 2 }]
},
{ "id": "O1002", "customerId": null, "placedAt": null, "items": null }
]
}
becomes:
using System;
using System.Collections.Generic;
public record Root(
IReadOnlyList<Customers> Customers,
IReadOnlyList<Orders> Orders);
public record Customers(
string Id,
string Name);
public record Orders(
string Id,
string? CustomerId,
DateTime? PlacedAt,
IReadOnlyList<Items>? Items);
public record Items(
string ProductId,
int Quantity);
How types are chosen
The JSON file is treated as the complete truth: types come only from the values in it.
| In the file |
C# |
1, 2 |
int (long if too large for int) |
8.0, 12.5 |
double (or decimal, see settings) |
1 and "1" |
int, since JsonSerializerOptions.Web reads numbers from strings |
1, "1" and "One" |
a union of int and string; "1" is read as int |
only strings, even "1" |
string |
| only ISO 8601 dates |
DateTime |
| only GUIDs |
Guid |
true and "true" |
a union of bool and string, since Web does not read booleans from strings |
any value plus null, or a property missing from some objects |
the same type with ? |
| a mix of objects, arrays and plain values |
a union of the inferred case types |
[] only, null only, or a number outside the supported numeric ranges |
JsonElement at the unknown position |
Mixed shapes
An incompatible array entry does not erase the types of its siblings. For example:
{
"orders": [
{ "id": "O1001", "quantity": 2 },
{ "id": "O1002", "quantity": "3" },
"O1003",
null
]
}
produces:
using System.Collections.Generic;
public record Root(IReadOnlyList<OrdersValue?> Orders);
public record Orders(string Id, int Quantity);
public union OrdersValue(string, Orders);
Quantity stays int: Web's built-in numeric-string conversion reads "3" as 3.
Object properties still merge across records, with missing properties made nullable. If items
contains an array in one order and an object in another, its union keeps both the typed list and
the typed object.
Number/text unions need a classifier because Web lets numeric cases read strings too. The generator
includes a JsonUnion attribute and classifier when needed. It probes a copy of the JSON reader with
the built-in numeric serializer, choosing the numeric case if it succeeds and the string case otherwise.
For example, "14.50" is read as a number while "€ 24,95" stays text. No custom serializer setup or
change to Web's number handling is needed.
Naming
- Records are named after their JSON key in PascalCase (
customers → Customers). The top-level record is Root.
- Unions add
Value to the key name, e.g. OrdersValue. Generated classifiers add Classifier.
Records, unions, and classifiers share the same collision-safe naming rules.
- Objects under the same key with the same shape share one record. A different shape under the same key
gets the parent's name as a prefix, e.g.
WishlistsItems.
[property: JsonPropertyName("...")] is added only when the default naming would not match the key,
as with first_name, user-id or 2fa.
- Keys that differ only in case (
id, Id) become one property, because JsonSerializerOptions.Web
matches names case-insensitively.
Requirements
- The generated code uses nullable annotations (
string?), so enable them in your project:
<Nullable>enable</Nullable>.
- Generated unions and their JSON support require C# 15 and .NET 11.
- JSON comments are not supported.
Settings
| Setting |
Default |
|
jsonToCSharp.outputFileName |
Types.cs |
File written next to the JSON file |
jsonToCSharp.rootTypeName |
Root |
Name of the top-level record |
jsonToCSharp.namespace |
(none) |
File-scoped namespace |
jsonToCSharp.collectionType |
IReadOnlyList |
IReadOnlyList or List |
jsonToCSharp.fractionalNumberType |
double |
double or decimal |
jsonToCSharp.dateType |
DateTime |
DateTime or DateTimeOffset |
jsonToCSharp.detectDates |
true |
Map ISO 8601 date strings to the date type |
jsonToCSharp.detectGuids |
true |
Map GUID strings to Guid |
License
MIT