A simple tool for converting JSON data into C# POCO (Plain Old CLR Object) classes with support for records, customizable property access, nullable types, and various collection types.
- Convert JSON to C# classes or records
- Support for nested objects and arrays
- Customizable property access patterns
- Optional JSON property name attributes
- Nullable type support
- Required property markers
- Default value initialization
- Multiple array type options
- Primary constructor support for records
Namespace: Set the namespace for generated classes (default: "JsonToCsharp")RootTypeName: Set the name for the root class/record (default: "Root")UseRecords: Generate C# records instead of classesUsePrimaryConstructor: Use primary constructor syntax for records (C# 9.0+)
-
PropertyAccess: Control property accessor patternsMutable: Generate{ get; set; }propertiesImmutable: Generate{ get; init; }properties (C# 9.0+)
-
ArrayType: Choose collection type for arraysIReadOnlyList<T>: Immutable list interfaceList<T>: Standard mutable listT[]: Array type
-
AddAttribute: Add[JsonPropertyName]attributes for JSON serialization -
IsNullable: Generate nullable reference types -
IsRequired: Addrequiredkeyword to properties (C# 11.0+) -
IsDefaultInitialized: Initialize properties with default values
Input JSON:
{
"name": "John",
"age": 30,
"isEmployee": true
}Settings:
var options = new ConversionSettings
{
Namespace = "MyNamespace",
UseRecords = false,
PropertyAccess = PropertyAccess.Mutable
};Output:
namespace MyNamespace;
public class Root
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("age")]
public int Age { get; set; }
[JsonPropertyName("isEmployee")]
public bool IsEmployee { get; set; }
}Settings:
var options = new ConversionSettings
{
UseRecords = true,
UsePrimaryConstructor = true,
AddAttribute = true
};Output:
public record Root(
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("age")] int Age,
[property: JsonPropertyName("isEmployee")] bool IsEmployee
);Settings:
var options = new ConversionSettings
{
IsNullable = true,
IsRequired = true,
PropertyAccess = PropertyAccess.Immutable
};Output:
public class Root
{
[JsonPropertyName("name")]
public required string? Name { get; init; }
[JsonPropertyName("age")]
public required int? Age { get; init; }
}Input JSON:
{
"items": ["A", "B", "C"]
}Settings for different array types:
// IReadOnlyList
options.ArrayType = ArrayType.IReadOnlyList;
// Output: public IReadOnlyList<string> Items { get; init; }
// List
options.ArrayType = ArrayType.List;
// Output: public List<string> Items { get; init; }
// Array
options.ArrayType = ArrayType.Array;
// Output: public string[] Items { get; init; }Settings:
var options = new ConversionSettings
{
IsDefaultInitialized = true,
PropertyAccess = PropertyAccess.Immutable
};Output:
public class Root
{
public string Name { get; init; } = string.Empty;
public IReadOnlyList<string> Tags { get; init; } = [];
public Address Address { get; init; } = new();
}The converter automatically maps JSON types to C# types:
- JSON strings →
string - JSON numbers →
intordouble - JSON booleans →
bool - JSON arrays →
IReadOnlyList<T>,List<T>, orT[] - JSON objects → Nested classes/records
- JSON dates →
DateTime