Turn JSON or XML Into Classes (DTOs) in Java, C# and More
Developer · Published October 17, 2026 · Updated October 24, 2026 · 3 min read
Written by Akinrinmade Michael
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. Generation is a good start, as long as you know what it can and can't see.
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.
A worked example
Here's a small response and the TypeScript the generator writes for it:
{"id": 7, "name": "Ada", "price": 9.5, "tags": ["a"], "address": {"city": "Leeds", "zip": null}}
export interface Order {
id: number;
name: string;
price: number;
tags: string[];
address: Address;
}
export interface Address {
city: string;
zip: unknown;
}In Java the same sample gives a class with an Integer id, a Double price, a List of String tags and an Address class, and zip comes out as a plain Object because a null tells the tool nothing. That one field shows the central limit of generation.
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's null in the sample has no known type.
- A whole number might need a long or a decimal in other responses. A price that's 10 in the sample may be 10.5 elsewhere.
- Dates arrive as text, so decide how to parse them.
- A list that's empty in the sample has an unknown item type.
- A field that holds one of a few fixed words is really an enum, but the generator only sees a string.
A checklist before you use the classes
- Generate from several real responses, including awkward ones, not a single tidy example.
- Fix every null or unknown type by hand.
- Change numbers to the widest type they could need, such as long for ids and decimal for money.
- Replace string fields with enums or date types where that's what they really are.
- Mark which fields are required, and add validation for them.
- Parse a few real responses with the new classes to check nothing is lost.
Naming and annotations
Follow your language's conventions in the class, and keep the original name in an annotation. That keeps your code tidy and still reads and writes the exact keys the API uses. Turn annotations on when your serializer needs them, such as Jackson in Java or the System.Text.Json attributes in C#. Without them, a field called user_name won't match userName unless your library is set to convert names.
Handling change over time
APIs add fields. A strict model that rejects unknown fields breaks the day the API adds one. Most serializers can be told to ignore unknown properties, which is the safer default for responses you don't control. For data you do control, such as your own API's requests, strictness helps catch mistakes.
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. Many tools can generate classes from a schema, which beats generating them from a sample.
Use the generated classes as a starting point. Add validation, and test them against real responses, before they go near production.
Related reading: when you need sample records to test the classes, see lorem ipsum and placeholder text for ways to make realistic dummy data.