Generate Kotlin Data Classes from API Response
This example generates Kotlin data classes with kotlinx.serialization annotations from a typical REST API response. The pre-filled JSON represents a user object with nested address, array of roles, and array of post objects. Switch to Kotlin mode to see the generated classes.
What You Get
The tool produces:
- A root
Rootdata class with@Serializableand@SerialNameannotations RootDataclass for the nested address objectRootPostsItemclass for array items@SerialName("key")attributes on every property- camelCase property names following Kotlin conventions
- Nullable types (
T?) with null defaults for optional fields - Kotlin type mappings:
String,Long,Double,Boolean,List<String>
How to Use
- The example loads with a sample API response and Kotlin mode pre-selected
- Inspect the generated Kotlin data classes and their annotations
- Copy the classes and paste them into your Kotlin project
- Replace with your actual API response to generate classes for your endpoints
Real-World Pattern
Kotlin data classes with kotlinx.serialization are the standard way to deserialize JSON in Android and Kotlin Multiplatform projects. The generated code works directly with Json.decodeFromString<T>(string). Use nullable types with null defaults for fields that may not always be present in the response.