JSON to C#
Convert JSON to C# classes or records online with System.Text.Json [JsonPropertyName] attributes, nullable types and List properties for arrays.
Something not working, or missing a feature? Send feedback
About the JSON to C#
The JSON to C# converter generates classes or positional records for System.Text.Json from a JSON sample. Property names use PascalCase with [JsonPropertyName] attributes, arrays become List<T>, and values that were null or missing become nullable types.
Conversion happens in your browser, so your files and records stay private.
How to use the JSON to C#
- Paste a JSON sample and set the root class name.
- Choose classes or records.
- Copy the code into your project and deserialize with JsonSerializer.Deserialize<T>().
Example
Input
{
"id": 7,
"fullName": "Grace Hopper",
"email": null,
"roles": ["admin"],
"team": { "name": "Compilers", "size": 4 },
"projects": [
{ "name": "COBOL", "year": 1959 },
{ "name": "A-0", "year": 1952, "archived": true }
]
}Result
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class User
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("fullName")]
public string FullName { get; set; } = string.Empty;
[JsonPropertyName("email")]
public object? Email { get; set; }
[JsonPropertyName("roles")]
public List<string> Roles { get; set; } = new();
[JsonPropertyName("team")]
public Team Team { get; set; } = new();
[JsonPropertyName("projects")]
public List<Project> Projects { get; set; } = new();
}
public class Project
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("year")]
public long Year { get; set; }
[JsonPropertyName("archived")]
public bool? Archived { get; set; }
}
public class Team
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("size")]
public long Size { get; set; }
}System.Text.Json matches names case-sensitively, so every property gets [JsonPropertyName]. Non-null strings and lists are initialised, so nullable reference types report no warnings.
Common mistakes
- Removing [JsonPropertyName]. Without it, System.Text.Json looks for "Id" and finds nothing for "id", leaving default values. Keep the attributes or set PropertyNameCaseInsensitive = true.
- Assuming long is enough. JSON numbers above 9,223,372,036,854,775,807 or with a fraction fail to deserialize into long. Use decimal or double for money and measurements.
Works well with the JSON to C#
- JSON Formatter: tidy and check the JSON before generating types.
- JSON to TypeScript: generate matching TypeScript interfaces for the front end.
- JSON Validator: find syntax errors in the sample first.
Frequently asked questions
Why does every property have [JsonPropertyName]?
System.Text.Json matches property names case-sensitively by default, so Id would not match "id". The attribute maps each property to its exact JSON key.
Does it work with Newtonsoft.Json?
Replace [JsonPropertyName] with [JsonProperty] and the using directive with Newtonsoft.Json; the rest of the code is the same.
What about nullable reference types?
Values that can be null are marked with ?, and non-null strings and lists are initialised (string.Empty, new()), so the code compiles without nullable warnings.
Related tools
JSON to CSV
Convert JSON arrays to CSV online with automatic column detection, nested object flattening and custom delimiters. Open the result in Excel or Sheets.
CSV to JSON
Convert CSV to a JSON array online with header detection, type inference and delimiter auto-detection. Turn spreadsheet exports into data for APIs and code.
JSON to XML
Convert JSON to well-formed XML online with a configurable root element and attribute support. Validate the input and download the XML file.
XML to JSON
Convert XML to JSON online, keeping attributes and turning repeated elements into arrays. Malformed XML is reported with its location.
JSON to YAML
Convert JSON to clean, human-readable YAML online. Turn API responses and package files into config for Kubernetes, Docker Compose or CI pipelines.
YAML to JSON
Convert YAML documents to formatted JSON online with syntax validation. Errors are reported by line, so broken indentation is quick to find and fix.