Turn JSON or XML Into Classes (DTOs) in Java, C# and More
Developer · October 17, 2026 · 1 min read
A data transfer object, or DTO, is a simple class that holds the data from an API response or a file: just fields, with no behavior. Writing them by hand for a large response is slow and easy to get wrong, so many developers generate them from a sample.
How the mapping works
- A JSON object becomes a class, and each key becomes a field.
- A JSON array becomes a list of the item type.
- Nested objects become their own classes.
- Strings, numbers and booleans map to the language's own types.
- Keys like user_name are turned into userName, and annotations keep the original name for reading and writing.
The JSON to DTO Generator writes Java (as plain classes, with Lombok or as records), C#, TypeScript, Kotlin, Python and Go. The XML to DTO Generator does the same for XML, where attributes and repeated elements need their own handling.
What a sample can't tell you
A generator only sees the sample you give it. Review these points before you use the classes:
- Optional fields that are missing from the sample will be missing from the class.
- A field that is null in the sample has no known type.
- A whole number might need a long or a decimal in other responses.
- Dates arrive as text, so decide how to parse them.
- A list that is empty in the sample has an unknown item type.
A schema is a stronger source
A JSON Schema describes which fields are required, what the types are and what values are allowed. You can start one from a sample with the JSON Schema Generator and then tighten it by hand. If you need to move data between formats, the XML to JSON Converter shows how the same data looks in each.
Use the generated classes as a starting point. Add validation, and test them against real responses, before they go near production.