JSON to Kotlin Data Class Generator
Paste JSON, get Kotlin data classes — nested objects, arrays, val/var, @Serializable — copy or download as .kt
Options
186 characters
2 classes generated · 332 characters
You’re writing an Android app or a Ktor backend. The API you’re integrating returns a JSON response with 20 nested fields. You need a Kotlin data class that maps to that shape so you can use kotlinx.serialization, Gson, or Moshi to deserialize it. Writing those classes by hand means PascalCase class names, camelCase property names, nested classes for each object, List<T> for arrays — and one wrong type means a runtime crash.
What Are Kotlin Data Classes?
Kotlin data class is a concise way to define a class whose primary purpose is to hold data. The compiler automatically generates equals(), hashCode(), toString(), and copy() methods based on the properties declared in the primary constructor:
data class User(
val id: Int,
val name: String,
val email: String
)
Data classes are the standard Kotlin pattern for modeling API responses, database records, and configuration objects. They work with all major Kotlin serialization libraries: kotlinx.serialization, Gson, Moshi, and Jackson.
Why Generate from JSON?
When integrating REST APIs, the response shape is defined by the server — not by you. Instead of manually translating each JSON field to a Kotlin property (and getting property types wrong), a generator reads the actual JSON and produces correct declarations automatically. This is especially useful when:
- The API has deeply nested objects (each level needs its own data class)
- The response has 30+ fields and writing them by hand is tedious
- You’re switching serialization libraries and need to rewrite annotations
- You’re onboarding to a codebase and need to model an unfamiliar API
This tool runs entirely in your browser. Your JSON data never leaves your device.
val vs var: Which Should You Use?
The val / var toggle controls whether generated properties are immutable or mutable.
val (recommended for API responses):
data class User(
val id: Int,
val name: String
)
Properties declared with val cannot be reassigned after construction. This is the default because API response objects are typically read-only data — you deserialize them once and read them. Immutability prevents accidental mutation and makes data classes easier to reason about in concurrent code.
var (use when you need mutation):
data class FormState(
var name: String,
var email: String
)
Use var when the data class represents mutable state — for example, form inputs or a ViewModel’s local copy that will be updated by user interaction. Note that data class copy() works with both: val properties can be updated via copy(name = "new name") without needing var.
kotlinx.serialization vs Moshi vs Gson
The @Serializable toggle adds the kotlinx.serialization annotation and the corresponding import. Here is a quick comparison of the three major Kotlin/Java JSON libraries:
| Library | Annotation | Null safety | Kotlin-first | Multiplatform |
|---|---|---|---|---|
| kotlinx.serialization | @Serializable | Yes | Yes | Yes |
| Moshi | @JsonClass(generateAdapter = true) | Yes | Yes | No |
| Gson | none (reflection-based) | No | No | No |
kotlinx.serialization is the Kotlin-native choice. It is compile-time safe, supports Kotlin Multiplatform (KMP/KMM), and has direct integration with Ktor. It requires the @Serializable annotation and the Kotlin serialization Gradle plugin.
Moshi is a popular Android choice. It uses code generation via @JsonClass and has excellent null safety. If you use Moshi, disable the @Serializable toggle and add the Moshi annotation manually.
Gson works without annotations through reflection. It is the simplest to set up but has no Kotlin null safety — a non-nullable String property will silently become null if the JSON key is missing. Avoid Gson for new Kotlin projects.
Null Safety in Generated Classes
When the nullable toggle is off (default), properties are generated as non-nullable Kotlin types (String, Int, Boolean). The generated classes assume the JSON will always provide these fields.
When nullable is on, all properties become nullable Kotlin types (String?, Int?, Boolean?). This is conservative — it means your code must handle null everywhere, but it won’t crash if a field is missing.
A practical middle ground: generate with non-nullable types first, then manually add ? only to fields you know can be absent. The generator marks properties as nullable automatically when a field is missing from some elements in an array of objects.
How to Use This Tool
- Paste your JSON in the JSON Input textarea (or click Load sample)
- Set your root class name (default:
Root) - Optionally enter a package name (e.g.,
com.example.model) - Choose
valorvarand toggle@Serializableas needed - Click Copy to copy to clipboard, or Download .kt to save as a
.ktfile - Paste the output into your project and adjust as needed
FAQ
Does this tool send my JSON to a server?
No. All processing happens entirely in your browser using JavaScript. Your JSON data never leaves your device.
How are nested objects handled?
Each nested object becomes its own data class. The class name is derived from the parent class name plus the property key in PascalCase. For example, an address property inside Root generates a class named RootAddress.
How are arrays handled?
Arrays of plain objects are merged into a single data class named after the property key plus Item (e.g., tags → List<String>, a nested object array posts → List<RootPostsItem>). Properties missing from some array elements are made nullable.
What does Any? mean in the output?
Any? is generated for null JSON values where the actual type cannot be inferred. It is a placeholder — replace it with the correct concrete type once you know what the field can contain (e.g., String?, Int?).
Can I use this output with Retrofit?
Yes. Retrofit with a Gson or Moshi converter factory, or Ktor with kotlinx.serialization, will deserialize JSON into the generated data classes automatically. Make sure to add the appropriate converter/plugin dependency in your build.gradle.kts.
What if my JSON root is an array?
The tool generates a typealias for the root array (e.g., typealias Root = List<RootItem>) and a separate data class for the element type.