JSON to Kotlin Data Class
Generate Kotlin data class definitions from a JSON sample.
Overview
The JSON-to-Kotlin converter produces Kotlin data class definitions from a sample JSON document. Field names are camelCased, types are inferred, and nullability is added based on whether values appear as null or missing in the source.
Android and JVM developers reach for a json to kotlin converter when consuming a REST API, parsing a webhook payload, or scaffolding model classes that pair with Moshi, kotlinx.serialization, or Gson. It saves the busywork of declaring every field by hand for a deeply nested response.
How it works
The generator walks the JSON tree, creates a data class for the root, and recursively creates classes for each nested object. JSON keys are camelCased into idiomatic Kotlin property names; the original spelling is preserved via a configurable annotation — @SerialName("...") for kotlinx.serialization or @Json(name = "...") for Moshi.
Type inference maps JSON to Kotlin: strings to String, integers to Int or Long, decimals to Double (or BigDecimal on opt-in), booleans to Boolean, arrays to List<T>. Null values and missing keys produce nullable properties (String?); always-present non-null values are non-nullable.
Examples
Sample:
{ "id": 1, "first_name": "Alice", "tags": ["admin","ops"], "address": { "city": "Berlin" } }
@Serializable
data class Root(
val id: Int,
@SerialName("first_name") val firstName: String,
val tags: List<String>,
val address: Address
)
@Serializable
data class Address(val city: String)
// Optional / nullable fields:
data class User(
val id: Int,
val email: String? = null,
val nickname: String? = null,
)
FAQ
Which serialisation library does the output target?
You can pick kotlinx.serialization (the modern default for multiplatform projects), Moshi (popular on Android), or Gson (legacy but widely deployed). The generator emits the right annotations and import hints for each.
Are non-null types preferred?
Yes. The generator only marks a property nullable when the sample data shows null or omits the key in some elements. That keeps your models tight and lets the compiler catch bugs.
Can I get sealed classes for polymorphic JSON?
Polymorphism (a discriminator field plus several shapes) needs explicit declaration in any serialisation library and isn't safely inferable from a single sample. Pair this with a manual @JsonClassDiscriminator after generation.