What JSON to C# does
JSON to C# turns a sample JSON document into C# code: one class with auto-properties per object, with a type for every field inferred from the value in the sample.
JSON to C# examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
public class Address
{
public string City { get; set; }
}
public class Root
{
public string Name { get; set; }
public bool Active { get; set; }
public List<string> Tags { get; set; }
public Address Address { get; set; }
}This is the input that the Load sample button fills in.
Awkward keys, a null, a decimal and arrays
Input
{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}Output
public class Items
{
public string Sku { get; set; }
public int Qty { get; set; }
public bool Gift { get; set; }
}
public class Root
{
public int OrderId { get; set; }
public double Total { get; set; }
public bool Paid { get; set; }
public object Coupon { get; set; }
public List<Items> Items { get; set; }
public List<object> Tags { get; set; }
}The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a C# type:
| Sample value | C# type |
|---|---|
| String | string |
| Whole number | int |
| Decimal number | double |
| true / false | bool |
| null, or an empty array’s items | object |
| Array of T | List<T> |
| Object | a new named type |
Using the generated code
With System.Text.Json, matching is case-sensitive by default and the generated names are capitalised, so pass the option below:
var root = JsonSerializer.Deserialize<Root>(json,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });Details and limits
- Add
using System.Collections.Generic;forList<T>andusing System.Text.Json;for the serialiser. - A key with punctuation is turned into PascalCase (
order-idbecomesOrderId) and then needs[JsonPropertyName("order-id")]. - A key that starts with a digit keeps the digit and is not a valid C# identifier until you rename it.
- With nullable reference types enabled, the compiler warns that the properties are not initialised; mark optional ones with
?. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
Is my data uploaded when I use JSON to C#?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does JSON to C# do?
JSON to C# turns a sample JSON document into C# code: one class with auto-properties per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
Add using System.Collections.Generic; for List<T> and using System.Text.Json; for the serialiser. A key with punctuation is turned into PascalCase (order-id becomes OrderId) and then needs [JsonPropertyName("order-id")].
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.