JSON to Kotlin
Paste a JSON sample and get Kotlin data classes, instantly and entirely in your browser. Optional fields, nullability and nested types are inferred from the sample — and anything the sample cannot settle is reported rather than guessed.
Type definitions will appear here
Kotlin data classes from a sample
Generates @Serializable data class declarations with @SerialName where the Kotlin name differs from the JSON key.
Optional and nullable fields are typed T? with a = null default — null safety is the point of the language, so it is not glossed over.
What a sample cannot tell you
Types are inferred from one sample. A field absent from your sample will be absent from the output, and a field that happens to hold only whole numbers will be typed as an integer even if it can hold decimals. Paste the widest sample you have. For a guarantee rather than an inference, start from a JSON Schema.
What Kotlin output looks like
Generated from two sample records, one of which has an extra field and a null. This is the actual output, not an illustration:
@file:Suppress("unused")
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Root(
val id: Long,
val userName: String,
val isActive: Boolean,
val score: Double,
val tags: List<String>,
val address: Address,
val lastSeen: String,
val nickname: String? = null
)
@Serializable
data class Address(
val city: String,
val postcode: Any? = null
)
The three things that differ by language
Large integers
Kotlin has Long, so a 64-bit identifier survives — unlike the JavaScript, Go v1 and TypeScript targets. Values beyond 253−1 are typed Long rather than Int.
Optional against nullable
Optional and nullable fields are typed T? with a = null default. Null safety is the point of the language, so this is not glossed over: a field that was missing from one sample is not declared non-null.
Names that are not identifiers
@SerialName is emitted whenever the Kotlin property name differs from the JSON key, so camelCase conventions do not break deserialisation.