C# ↔ JSON
Generate sample JSON from C# classes (loose mode) or generate C# DTOs from JSON.
Generate sample JSON from a C# model, or a DTO from JSON
When you're wiring up an API client or writing a test fixture, you often need sample JSON that matches a C# model, or the reverse: a DTO class for a JSON payload you were handed. This tool tokenizes pasted C# to find classes, records and structs and their public properties, then fills each property with a default or example value. It tolerates namespaces, usings, attributes and non-compiling snippets, since it parses loosely rather than compiling.
How to use the C# ↔ JSON
- Paste a C# class, record or struct into the left panel in C# → JSON mode, or click Example for a UserResponse model with a nested AddressDto.
- Toggle camelCase to lowercase the first letter of each property name, matching System.Text.Json's default naming policy.
- Toggle Example values to fill scalars with representative data like "string", 123.45 or a sample GUID, instead of empty defaults.
- Switch to JSON → C# mode, paste or generate JSON, and optionally check Use records to emit record types instead of classes.
- Copy the generated code, or use Send To to carry the JSON output into another converter such as JSON Schema.
The C# → JSON direction picks a root type automatically: the class not referenced as a property type by any other parsed class. Nullable types (T? or Nullable<T>) become JSON null, and Dictionary<TKey,TValue> becomes an empty JSON object.
JSON → C# infers int vs long by magnitude, bool from JSON booleans, and List<T> from arrays, adding a [JsonPropertyName("...")] attribute whenever the generated PascalCase property name differs from the original JSON key.
Frequently asked questions
- Does this tool actually compile the C# code?
- No, it uses a tokenizer and brace matcher to find class, record and struct declarations and their properties, so it works on incomplete snippets, missing usings or code with compile errors, unlike a Roslyn-based generator.
- How does it decide default values for each property type?
- Strings default to an empty string, numbers to 0, booleans to false and DateTime-family types to an empty string, unless you enable Example values, which fills a representative value like "string", 123.45 or 2024-01-01T00:00:00Z instead.
- Can it generate C# from a JSON array?
- Yes, in JSON → C# mode it looks at the first object in the array to infer the class shape, so an array of similar objects produces one class definition rather than one per array element.
- Does it add JsonPropertyName attributes automatically?
- Only when the PascalCase C# property name it generates differs from the original JSON key, for example a snake_case or camelCase key, so the resulting class still serializes back to the exact same JSON shape.