Code Generators
JSON to Kotlin Converter
Paste a JSON or XML sample — the format is auto-detected — and get typed Kotlin models with names, nesting and nullability inferred for you.
In short
Convert JSON to Kotlin online: paste a sample and generate typed Kotlin models with serialization helpers — powered by quicktype, in your browser.
By NaveenKumar T · Updated
- JSON or XML input, auto-detected
- Nested objects become types
- Nullability inferred
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
Example: JSON order payload to Kotlin
A small order document with a nested object, an array of two items, a decimal total and a null field — enough to show how each Kotlin type, list and nullable is inferred. The same page accepts the XML form of this order too, so the sample below is an example rather than the only accepted format. The right-hand side is the exact output of this tool.
{
"id": "ord_9F2KX",
"status": "processing",
"paid": true,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com", "phone": null },
"items": [
{ "sku": "KB-75", "qty": 1, "price": 149.99 },
{ "sku": "MS-12", "qty": 2, "price": 39.5 }
],
"total": 228.99
}// To parse the JSON, install Klaxon and do:
//
// val root = Root.fromJson(jsonString)
package quicktype
import com.beust.klaxon.*
private val klaxon = Klaxon()
data class Root (
val id: String,
val status: String,
val paid: Boolean,
val customer: Customer,
val items: List<Item>,
val total: Double
) {
public fun toJson() = klaxon.toJsonString(this)
companion object {
public fun fromJson(json: String) = klaxon.parse<Root>(json)
}
}
data class Customer (
val name: String,
val email: String,
val phone: Any? = null
)
data class Item (
val sku: String,
val qty: Long,
val price: Double
)
Look for the "phone" field: sampled as null, it is typed as Any?. "items" became List<Item>, and "total" is Double.
Learn more
From JSON sample to Kotlin types
Hand-writing model classes from a JSON payload is transcription work: read a field, type a property, repeat eighty times, miss one. This page runs quicktype (the same engine behind the best-known generators) against your document, merging array elements to find the common shape and naming nested types from their keys.
Klaxon populates the data classes by reflection, and R8/ProGuard strips unused constructors and renames properties in a release build, so models that work fine in debug fail at runtime in production. Add @Keep (or a keep rule for the generated package) before the first release, or switch the companion helpers to kotlinx.serialization, which needs no reflection.
How JSON values become Kotlin types
Inference works from the values in your sample. Strings map to String, integers and decimals are told apart (228.99 becomes Double, while 1 and 2 stay integral), true/false become the language's boolean, and an object such as "customer" becomes its own named type. The "items" array is typed as List<Item> because every element shares the same keys — quicktype unifies the shapes of all array elements into one Item type.
Nulls are the one place inference needs your help: "phone": null carries no type information, so the field is emitted as Any?. If the real API sometimes sends a string there, include a sample where the field is populated (paste several documents merged into one array) and the generated type becomes the union of what it saw.
Pasting XML instead: how elements and attributes become Kotlin types
The XML is parsed into a data model before inference (attributes included, text nodes named "text"), so the root element becomes a nested type: <order> is a field of type Order on the top-level Root rather than the top level itself. Child elements become fields, and repeated elements — the two <item> siblings — are collected into List<Item> under an Items wrapper type that mirrors the <items> container.
Attributes and element text are typed differently, and this matters: element text is type-inferred, so <total>228.99</total> becomes Double, while attribute values are kept as they were written — qty="1" and price="149.99" stay String in Kotlin. Prefer element text for numeric data you want typed, or cast attribute values after decoding. One more trap: a single <item> would not be recognized as a list, so make sure repeated elements appear at least twice in the sample.
Annotations, nulls and nested arrays in the generated Kotlin
The data classes target Klaxon: a private Klaxon instance backs Root.fromJson and root.toJson, generated on the top-level class only. Renames use Klaxon's @Json annotation carrying name = "order_id" above the property, and it is emitted only where the Kotlin name differs from the key — a sample of clean lowercase keys therefore produces annotation-free data classes. Nothing here is kotlinx.serialization or Moshi: if that is your stack, add @Serializable or @JsonClass to the same data classes and drop the generated companion helpers.
A property whose only sampled value is null is typed Any? with a null default — nullable, but with no type behind it. Add a document where the value is present and it becomes String? = null, a real nullable whose default lets Klaxon construct the class when the key is absent. Fields missing from some of the merged samples get the same treatment. Everything else is declared non-null, so if the live API can omit a key, include a sample without it rather than trusting the generated signature.
Nested objects become their own data classes named from the key — Customer, Item — and arrays of objects are typed List<Item>. Only the outermost class gets the companion fromJson and the toJson method, so nested types are built for you rather than parsed directly; Klaxon reflects over the constructors, which is why the property names and defaults matter more than they look. The array traps are unchanged: one element hides optional fields, and an empty array becomes a List of nullable Any with no element class.
Help
Frequently asked questions
Everything you need to know about the JSON to Kotlin.
01How do I generate Kotlin classes from JSON?
Paste a representative document — JSON or XML, the format is detected automatically — and press Convert. A real API response works best. Nested objects become their own Kotlin types, arrays become List<Item>, and the result downloads as Root.kt.
02What does the generated Kotlin code include?
Kotlin models generate as data classes with nullable types where the sample showed nulls — concise constructor-property syntax that drops into Android or server-side Kotlin unchanged.
03How do I use the generated Kotlin code in my project?
Add Klaxon (com.beust:klaxon) to your build, drop the file into the quicktype package (or rename it), and call Root.fromJson(jsonString); root.toJson() serializes. The data classes themselves are library-neutral — swap the companion helpers for kotlinx.serialization or Moshi if that is what your project uses.
04Which serialization library do the Kotlin classes target?
The data classes themselves are library-neutral; generation includes Jackson-flavored helpers by default. Using kotlinx.serialization or Moshi? Add @Serializable/@JsonClass to the same classes — the structure transfers as-is.
Keep working