JSON to C#: Differences, Edge Cases & Pitfalls
Generate C# classes from JSON with PascalCase properties and JsonPropertyName mappings.
Turning a JSON sample into C# classes emits PascalCase properties with attributes mapping the original keys. Review the output by hand: numbers default to double and destroy big integers, empty arrays only produce placeholder object[], and nullability must match the real contract.
Format Differences
| Aspect | JSON | C# type definition |
|---|---|---|
| Typing | Weak; resolved at runtime | Strong; checked at compile time |
| Naming | camelCase / snake_case mixed | PascalCase properties plus mapping attributes |
| null handling | null is just a value | Value types need Nullable<T> or the ? suffix |
| Arrays | Mixed element types allowed | Strongly typed collections (List<T>, T[]) |
| Number precision | Large integers may be float-damaged | long / decimal hold them exactly |
| Dates | String or epoch number | DateTime / DateTimeOffset with clear semantics |
| Absence | Missing field and null are indistinguishable | Nullable reference types make it explicit |
| Runtime cost | Cheap to parse | Deserialisation allocates objects |
Edge Cases
- Inferring every number as double damages integers beyond 2^53 — change that property to long or decimal by hand.
- One field that is a number in some objects and a string in others breaks inference; emit object or handle both.
- Keys with hyphens or dots cannot be identifiers, so mapping attributes are the only bridge.
- An empty array gives no element type, producing object[] or List<object> that must be replaced manually.
Common Pitfalls
- Deleting the generated mapping attributes leaves every field at its default after deserialisation, with no error.
- Big integers inferred as double zero out the last digit of an order number, discovered only at the database write.
- Empty arrays infer object[], which compiles but forces casts everywhere downstream.
- A field with inconsistent types across samples generates a type matching only one of them, throwing at runtime.
Related Tools
- JSON to C#Generate C# classes from sample JSON with nested types, List generics and JsonProperty attributes for .NET models, splitting nested objects into their own.
- Data Format ConverterConvert between JSON, YAML, TOML, XML and CSV in one place with a single paste and instant preview for seamless multi-format config and data conversion.
- JSON to YAMLConvert JSON data to YAML format with custom indentation and nested object support. Turn verbose JSON configs into clean readable YAML in a couple of seconds.
- Excel to JSONConvert Excel files (xlsx, xls, csv) to JSON format. Supports nested headers, sheet selection and key mapping. Choose the sheet and header row and keep numbers.