How to convert JSON to C#
- Paste a JSON object or array, for example a response from the API you are calling.
- Set the name of the root type.
- Copy the generated C# code into your project.
What you get
- Classes with auto-properties and
[JsonPropertyName]for System.Text.Json. - PascalCase property names,
List<T>for arrays andlongordoublefor numbers. - Nullable types (
bool?,string?) for optional and null values, and initial values for the rest.
Tips for accurate results
The generator only knows the sample you paste. Include arrays with several items and responses where optional fields are present, so it can tell which keys are always there, which can be null and which numbers have decimals.
Frequently asked questions
Is my JSON sent to a server?
No. The code is generated in your browser, so API responses and other private data never leave your device.
How do I deserialize the JSON?
Use JsonSerializer.Deserialize<Root>(json) from System.Text.Json. The JsonPropertyName attributes map each JSON key to its property, so no naming policy is needed.
Does it work with Newtonsoft.Json?
Replace [JsonPropertyName("x")] with [JsonProperty("x")] and the using with Newtonsoft.Json; the rest of the classes are the same.
Why do some properties have = "" or = new()?
With nullable reference types enabled (the default in modern .NET), non-nullable properties need a value so the compiler does not warn about possible nulls.