JSON1

JSON1 / JSON to C#

JSON to C#

Generate C# classes with System.Text.Json attributes from a JSON sample. Properties are PascalCase with a JsonPropertyName holding the original key, and optional value types get a question mark.

Processed locally
InputJSON
Ready
OutputC#

Converted output appears here as you type.

Good to know

  • Every record in an array is inspected, not just the first, so a field that appears in only some of them is typed as optional rather than required.
  • Identical nested shapes are emitted once and reused, and reserved words are renamed to something legal in this language.

About this tool

C# classes for a JSON payload need attributes to bridge the naming conventions, since .NET properties are PascalCase and JSON keys usually are not. This generates the properties and the attributes together.

Nested objects become their own classes and System.Collections.Generic is imported only when the payload contains a list.

Why does every property have a JsonPropertyName?
Because PascalCase properties almost never match their JSON keys exactly. Carrying the original key in the attribute means serialisation works without relying on a global naming policy being configured.
Why System.Text.Json and not Newtonsoft?
System.Text.Json ships with .NET Core 3.0 and later, so the attributes resolve without a package reference. Newtonsoft is still common in older projects — if that is where this is going, the change is JsonPropertyName to JsonProperty and a different using.
How are optional fields decided?
Every record in an array is inspected, not just the first. A field that appears in some records and not others is typed as optional, and a value that is sometimes null widens accordingly. Generators that read only the first element get this wrong on exactly the payloads where it matters.
What happens to a key that is a reserved word?
It is renamed to something legal in the target language, and where the language supports it, an annotation records the original JSON key so serialisation still round-trips.