JSON to Kotlin Data Class
Convert JSON to Kotlin data classes with optional serialization annotations.
Options
data class Root(
val userId: Int,
val firstName: String,
val lastName: String,
val email: String,
val isActive: Boolean,
val score: Double,
val roles: List<String>,
val address: Address,
val createdAt: String
)
data class Address(
val street: String,
val city: String,
val state: String,
val zipCode: String
)Kotlin data classes for JSON: the type-safe approach to Android API parsing
Android development before Kotlin often meant parsing JSON with JSONObject and a mix of getString(), optInt(), and nested null checks: verbose, brittle, and not type-safe. Kotlin data classes combined with a serialization library solve all of this. Define a data class that mirrors your API response shape, and the library handles all the deserialization boilerplate. If the JSON structure changes and your model no longer matches, you get a compile-time error rather than a runtime crash.
The choice of serialization library matters for Kotlin Multiplatform projects. kotlinx.serialization is the only option that works on all Kotlin targets (JVM, Android, iOS via Kotlin/Native, JavaScript). Gson and Moshi are JVM/Android-only. If there is any chance your model will be shared in a KMP project, start with kotlinx.serialization from day one.
How to convert JSON to a Kotlin data class: step by step
- 1Paste your JSON
Copy any valid JSON object and paste it into the left input area. Nested objects generate inner data classes automatically. - 2Set the root class name
Type a PascalCase name for the top-level Kotlin class: for example UserResponse or ApiResult. Nested class names derive from their JSON key. - 3Choose a serialization library
Select none (plain data class), Gson (@SerializedName annotations), Moshi (@Json annotations), or kotlinx.serialization (@Serializable + @SerialName). The output includes the appropriate imports. - 4Choose val or var properties
val creates immutable properties (recommended for response models). var creates mutable properties (useful if you modify the object after creation). - 5Copy and paste into Android Studio
Click Copy to grab the generated Kotlin code and paste it into a new .kt file. The data class is ready to use with your chosen serialization library.
Related Tools
JSON to TypeScript
Convert JSON objects into TypeScript interfaces or type aliases. RN-friendly types, optional fields, readonly, and multiple export styles.
JSON to Go Struct
Generate Go structs from JSON with json tags, omitempty, pointer types for nullables, and proper PascalCase field name conversion.
CSS to React Native
Convert CSS rules to React Native StyleSheet objects. Handles shorthand expansion, property mapping, and flags unsupported properties.
RN Shadow Generator
Visually generate React Native shadow props for iOS and Android. Preview shadows in real time and copy platform-specific code.
Frequently Asked Questions
- What is a Kotlin data class?
- A data class is a Kotlin class prefixed with the data keyword. The compiler automatically generates equals(), hashCode(), toString(), copy(), and componentN() functions from the constructor properties: making it ideal for immutable model objects like API responses.
- Which serialization library should I choose for Android?
- kotlinx.serialization is the recommended choice for new Kotlin projects: it is 100% Kotlin, works with Kotlin Multiplatform, and does not require reflection. Gson is the most established Android library and works well for pure Android projects. Moshi is a good middle ground with both reflection and code-gen options.
- When should I use val vs var for data class properties?
- Use val for immutable properties: this is the standard practice for API response models and prevents accidental mutation. Use var if you need to update properties after creation, for example in a form view model.
- How are null JSON values handled?
- JSON fields with null values generate nullable Kotlin types (String?, Int?). This accurately reflects that the server may omit or null the field at runtime.
- How does the generator handle nested objects?
- Nested JSON objects generate inner data classes with a name derived from the JSON key in PascalCase. They are declared as top-level classes in the same file for clean Kotlin structure.
- What annotations does Gson mode add?
- @SerializedName("json_key") is added to properties whose JSON key differs from the Kotlin property name (snake_case to camelCase conversion). This tells Gson to map the JSON field correctly.
- Does kotlinx.serialization mode add @Serializable?
- Yes. The generated class is annotated with @Serializable (required for kotlinx.serialization to process it) and @SerialName is added for properties with non-matching JSON keys.
- Is this tool free?
- Completely free. The conversion runs entirely in your browser: no data is sent to any server.
AlteredIdea vs alternatives
vs other online tools: Everything runs in your browser: private, instant, no account needed.
vs desktop apps: No install required. Works on any device.
vs paid tools: Completely free, unlimited use.