JSON to C#
Generate C# classes from a JSON sample: nullable annotations, List types, optional System.Text.Json attributes and four naming conventions.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
#nullable enable
using System.Collections.Generic;
namespace JsonModels
{
public class RootOwner
{
public string Name { get; set; }
public object Email { get; set; }
}
public class RootItems
{
public string Sku { get; set; }
public double Price { get; set; }
public string? Note { get; set; }
}
public class Root
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public double Score { get; set; }
public RootOwner Owner { get; set; }
public List<string> Tags { get; set; }
public List<RootItems> Items { get; set; }
public object Meta { get; set; }
}
}Summary: Types 3 · Fields 13 · Name clashes 0
What this tool does
- Talk to a .NET backend or write Unity scripts: turn an API response into C# data classes instead of hand-writing the POCOs.
- Keep naming consistent: emit fields as PascalCase, camelCase, snake_case or kebab-case, with C# keywords automatically escaped by an underscore.
- Preserve the original keys: with [JsonPropertyName] enabled, serialization uses the JSON key while the property still follows C# conventions.
- Handle nulls and heterogeneous data: null fields become nullable types, arrays become List<T> and mixed arrays fall back to object.
Example
Input
Root class Root, namespace JsonModels, field naming PascalCase, JSON {"id":1,"name":"UniKit","is_active":true,"owner":{"name":"Ada","email":null}}Output
#nullable enable
namespace JsonModels
{
public class RootOwner
{
public string Name { get; set; }
public object Email { get; set; }
}
public class Root
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public RootOwner Owner { get; set; }
}
}Two classes and six fields in total. email is null in the sample, so it is inferred as object; change it to string? by hand if you want a precise type. Enabling [JsonPropertyName] adds a line such as [JsonPropertyName("is_active")] above each property and pulls in using System.Text.Json.Serialization.
Frequently asked questions
How is this different from the C# output of the JSON to code tool?
This one is C#-specific: it adds a namespace, four field naming conventions, a [JsonPropertyName] switch, C# keyword escaping (keywords get an underscore) and a naming-conflict report. Use the JSON to code tool when you need TypeScript, Java, Go and the rest.
Why does a null field become object?
Because the sample value is null and the tool has no way to know the real type, so it falls back to the permissive object type. The fix is easy: replace null with a representative value such as "ada@example.com" and regenerate, or change it to string? afterwards.
What are nullable reference types and #nullable enable?
The generated file starts with #nullable enable, which turns on the nullable reference type context. Missing fields then get a ?, as in public string? Note { get; set; }, so C# 8 and later warns you about possible null references at compile time instead of throwing NullReferenceException at runtime.
What happens when field names collide?
Two different JSON keys can collapse into the same name once a naming convention is applied — is_active and isActive, for example. The tool appends a numeric suffix to the later field and lists the conflict as "original key → final field name" in the summary so you can decide what to keep.
Can the root be an array?
Yes, as long as it is an array of objects: one class is generated from the element shape plus a wrapping List<T>. If the array mixes scalars or different shapes you get "the root value must be a JSON object or an array of objects". Parsing and generation are local, nothing is uploaded.
Keywords:jsonc#csharppocosystem.text.jsonjsonpropertynameJSON 转 C#数据类可空引用类型代码生成