JSON to C#
Generate C# classes from a JSON sample with JsonPropertyName attributes, nullable fields and List<T>. Parsing stays in your browser.
C# · C# 12 / .NET 8. Parsing and type inference happen in this browser tab. Nothing is uploaded and no network request is made.
Is this tool not working?
Tell us what went wrong and we’ll fix it. The report is sent with the page you’re on (/json-to-csharp/), so you don’t need to name the tool.
About the JSON to C#
JSON to C# turns a pasted JSON sample into plain C# model classes you can drop into a .NET project. It reads the sample in your browser, infers a type for every value it finds, and emits classes with PascalCase properties, System.Text.Json.Serialization.JsonPropertyName attributes that preserve the original JSON keys, and nullable annotations for fields that were null or absent. Root and nested type names are derived from your key names: profile becomes Profile, a list of users becomes User, and two differently shaped nested objects that would otherwise collide receive distinct names instead of overwriting one another. Arrays become List<T> and a nested object becomes its own class rather than a generic dictionary, so you get discoverable properties and compile-time checking in the IDE. Numbers deliberately map to double rather than guessing int, long or decimal from one sample value, because 42 on its own says nothing about the range or precision an API may eventually send. Strings stay string, booleans stay bool, and a value that is null or that mixes incompatible types in an array falls back to JsonElement, the general-purpose System.Text.Json type, so the generated class still compiles when the sample is ambiguous. A top-level array of objects is handled too: the element type is generated and a comment records that the JSON root was an array. The generated file enables nullable reference types itself, so string? remains meaningful even if your project has not switched that setting on. Inference is sample-based, so treat the output as a starting point rather than a contract. A key missing from this one response is marked optional with a nullable type; an empty array gives no clue about its element type; and nothing here validates future payloads against the generated class. The tool does not infer DateTime from ISO strings, enums from a handful of values, inheritance, required-field rules or unknown extra keys, and it does not consult the API documentation. Reserved C# keywords are escaped where necessary, a property that would clash with its enclosing class is renamed, and duplicate names arising from different spellings of the same key get numeric suffixes with their original JSON names kept in the attributes. Nothing is uploaded: JSON.parse and the emitter both run in the tab, so a sensitive production sample never leaves your device. Use the result as a first draft for DTOs in an API client, then tighten nullability, choose concrete numeric types and add real validation based on the service you are calling. A generated class is convenient scaffolding, not a substitute for a documented schema, and reviewing it before you commit is faster than debugging an incorrect assumption later.
How to use it
- Paste a JSON object, or an array of objects, into the JSON input box. The Load example button inserts a labelled sample you can replace.
- Type the name for the top-level type in Root type name; Root is the default and nested names are derived from your keys.
- Press Generate C#. The source appears in the result box; Ctrl/⌘ + Enter in the input does the same thing.
- Read the generated classes and attributes, then copy them or download the .cs file with the root type name as its filename.
- Adjust nullability and numeric types to match the real API contract before you use the classes in a project.
Worked example
Good to know
This tool processes everything locally with JavaScript. Nothing you paste or upload is transmitted to Hellagape or to any third party, and closing the tab clears the data. There is no daily limit and no account requirement. Results are provided for information only; for legal, financial or medical decisions, confirm the outcome with a qualified professional.
FAQ
JSON to C# questions
Why are all numbers generated as double?
A single sample cannot tell you whether a value is an int, a long, a decimal or a floating-point number. 42 could be an identifier that must stay an integer, and 19.99 could be money that needs decimal. Using double avoids a confident but wrong guess; narrow the types yourself once you know the API contract.
What happens to fields that are missing from my sample?
When several objects in an array are merged, a key present in only some of them is marked optional and emitted with a nullable type. With only one object there is no way to know whether a key is optional, so the tool assumes the shape you pasted is complete. Check the real responses before relying on that.
What type is used for null or mixed values?
JsonElement from System.Text.Json. It can hold any JSON value and keeps the class compiling when the sample gives no usable type information. A null field becomes JsonElement?, and an empty array becomes List<JsonElement> because its element type cannot be inferred from zero items.
Does it generate records, DateTime or enums?
No. It emits ordinary classes with settable properties, keeps every string as string and does not guess that a small set of values is an enum. Those are design decisions that need knowledge of the service, not a single response, so the tool leaves them to you.
Is my JSON sent anywhere?
No. Parsing, schema inference and code emission all run in JavaScript in your browser tab. There is no upload, no account and no server-side copy of the payload, so you can safely paste internal API responses.
Will this validate real API responses?
No. The generated classes describe one sample; System.Text.Json will still ignore unknown keys and throw only when a value cannot be converted to its declared type. Add explicit validation or a hand-written schema if you need to enforce required fields, ranges or exact formats.
Keep going