What is a JSON to Kotlin converter?
Hand-writing data classes for an API response is slow and error-prone. This tool infers Kotlin data classes from a JSON sample: nested objects become named types, arrays become typed collections, and null/missing values become optional fields. It runs quicktype's inference engine entirely in your browser, so real API payloads (often containing user data or credentials) never leave your machine.
Other languages from the same JSON
Kotlin mapping notes
- Kotlin
data classes annotated with@Serializablefor kotlinx.serialization, the official Kotlin JSON library. - Keys that aren't valid Kotlin camelCase keep their JSON name via
@SerialName("full_name"), so snake_case APIs map correctly. - JSON integers map to
Longand decimals toDouble; nullable or missing fields becomeT? = null.
Set up kotlinx.serialization
Add the serialization plugin and the JSON runtime to build.gradle.kts (match the plugin version to your Kotlin version):
plugins {
kotlin("plugin.serialization") version "2.0.21"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
}Then decode and encode with a configured Json instance:
val json = Json { ignoreUnknownKeys = true }
val root = json.decodeFromString<Root>(body)
println(root.owner.email)
val out: String = json.encodeToString(root)Common Kotlin JSON pitfalls
- Unknown keys throw by default. kotlinx.serialization fails on any key that isn't in the class, so an API adding a field breaks your app. Use
Json { ignoreUnknownKeys = true }as shown above. - Missing vs null. A nullable property needs a default (
= null) to tolerate the key being absent, not justnull. The generator adds it for fields that are missing in part of your sample. - Int vs Long. Integers are typed
Longto be safe with large IDs; change toIntif you prefer. - Android + R8. The serialization plugin generates serializers at compile time, so no reflection or ProGuard keep rules are needed (unlike Gson).
How to use
- Paste a JSON sample (an API response works well) into the Input pane.
- Generated Kotlin data classes appear instantly. Nested objects become their own named types.
- Non-standard JSON (single quotes, trailing commas, comments) is auto-repaired first.
- Copy the code into your project.
Examples
JSON → Kotlin data classes
{
"id": 42,
"name": "workbench",
"owner": { "email": "[email protected]", "active": true }
}@Serializable
data class Root (
val id: Long,
val name: String,
val owner: Owner
)
@Serializable
data class Owner (
val email: String,
val active: Boolean
)FAQ
How do I convert JSON to Kotlin types?
Paste any JSON sample. The data classes are inferred from the values and nesting, entirely in your browser. Nothing is uploaded.
How are nested objects handled?
Each distinct nested object becomes its own named type, referenced from the parent, arrays of objects included.
What about optional or null fields?
Fields that are null or missing in parts of the sample are typed as optional/nullable in the generated code.
Can I paste multiple samples?
Paste an array of objects: the type is inferred from the union of all items, which catches optional fields a single sample would miss.
Can I use Gson or Moshi instead?
Yes. Drop the @Serializable annotations and turn each @SerialName("x") into @SerializedName("x") (Gson) or @Json(name = "x") (Moshi). The data classes themselves stay the same.
How do I decode a JSON array into a List?
Paste the array as your sample, then call json.decodeFromString<List<RootElement>>(body).
Does it work on Android and Kotlin Multiplatform?
Yes. kotlinx.serialization is multiplatform (JVM, Android, iOS, JS), so the same data classes work in shared KMP code.
Is my JSON uploaded?
No. Generation runs 100% in your browser via quicktype's engine. Nothing is uploaded.