Generate Kotlin data classes from any JSON instantly. Get idiomatic Kotlin code with proper nullability, optional kotlinx.serialization or Gson annotations, and correct type inference. Free, no signup required.
Open JSON to Kotlin Generator →Kotlin data classes are a powerful language feature designed specifically for holding data. By prefixing a class declaration with the data keyword, the Kotlin compiler automatically generates several critical methods based on the primary constructor properties: equals() and hashCode() for value equality, toString() for human-readable output, copy() for creating modified copies, and componentN() functions for destructuring declarations.
Compare a Java POJO for a user with three fields (requiring a constructor, six getter/setter methods, equals, hashCode, and toString - approximately 50 lines of boilerplate) to the equivalent Kotlin data class:
data class User(
val id: Int,
val name: String,
val email: String,
val isActive: Boolean = true
)
One line of Kotlin replaces dozens of lines of Java. The data keyword gives you structural equality, a useful toString, and a copy function automatically.
Data classes also integrate seamlessly with Kotlin's null safety system. Properties declared as val name: String are guaranteed non-null at compile time. Properties declared as val name: String? can be null and must be handled explicitly with null checks, the safe call operator ?., or the Elvis operator ?:. This eliminates an entire class of NullPointerException bugs that plague Java codebases.
Two serialization libraries dominate JSON handling in Kotlin projects. The choice between them depends on your project type and constraints.
kotlinx.serialization is the officially supported library from JetBrains. It is the recommended choice for all new Kotlin and Android projects. Key characteristics:
@Serializable on the class and @SerialName("json_key") on propertiesGson is the older Java library from Google, widely used in Android apps before kotlinx.serialization matured. Key characteristics:
@Keep annotations or ProGuard rules to prevent obfuscation from breaking it@SerializedName("json_key") on propertiesFor any new Android or Kotlin backend project starting today, kotlinx.serialization is the clear choice. For existing Retrofit-based Android apps already using Gson, migrating is beneficial but not urgent.
Generating Kotlin data classes from a JSON payload takes only seconds:
For kotlinx.serialization, add to your build.gradle.kts:
plugins {
kotlin("plugin.serialization") version "1.9.0"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}
Then annotate your data class with @Serializable and deserialize with Json.decodeFromString<MyClass>(jsonString).
Android development is one of the most common contexts for converting JSON to Kotlin data classes. Here are the key scenarios where this generator saves significant time:
@Keep and using the Firebase Kotlin extensions.Kotlin data classes are classes prefixed with the data keyword that the compiler treats specially. The compiler automatically generates equals(), hashCode(), toString(), and copy() methods based on the properties declared in the primary constructor. They are the idiomatic Kotlin way to represent value objects and JSON models - far more concise than equivalent Java POJOs. Data classes work directly with Kotlin's null safety system, allowing you to express nullable vs non-nullable fields at the type level.
kotlinx.serialization is the official Kotlin library that processes annotations at compile time via a compiler plugin. It fully understands Kotlin's type system including nullability and default parameter values, works with code obfuscation (R8/ProGuard) without special rules, and supports Kotlin Multiplatform. Gson is a Java reflection-based library that does not understand Kotlin nullability and can silently violate null contracts. For any new Kotlin project, kotlinx.serialization is strongly preferred. Gson is only appropriate for legacy Android codebases where migration is not feasible.
JSON fields that contain null values or that may be absent from the response are generated as nullable Kotlin types, such as String?, Int?, or Boolean?. Fields that have a consistent value across the sample JSON are generated as non-nullable types. Optional fields that may be missing are given default values (typically = null for nullable types) so that kotlinx.serialization can deserialize partial JSON without a MissingFieldException. You can adjust nullability after generation based on your knowledge of the actual API contract.
Each nested JSON object generates a separate Kotlin data class. The parent data class holds a property of the nested class type. For example, a JSON field "profile": {"avatarUrl": "...", "bio": "..."} generates a Profile data class and a val profile: Profile property in the parent. JSON arrays of objects generate List<T> properties. All data classes are output together so you can copy the entire block into a single Kotlin file. The @SerialName annotation is added automatically whenever a JSON key uses snake_case and the Kotlin property name uses camelCase.
Free, instant, 100% private. No account needed.
Open JSON to Kotlin Generator →Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Java | JSON to Go | JSON to C# | JSON to PHP