JSON to Kotlin data class: from JSON to code
Paste JSON and get Kotlin data classes for kotlinx.serialization (the default) or Gson. Properties are camelCase with @SerialName to keep the original key, numbers are Int or Double, lists List<T>, and optional values are nullable with a default of null so that a missing key does not fail the parsing.
This page opens the converter on Kotlin. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.
A real conversion
The tool opens with this JSON, an order with a customer and two items:
{
"id": 48213,
"status": "paid",
"paid_at": "2026-11-21T14:03:00Z",
"total": 389.3,
"coupon": null,
"customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
"items": [
{ "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
{ "sku": "MS-07", "qty": 1, "price": 89.5 }
]
}and this is what it writes for Kotlin:
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Root(
val id: Int,
val status: String,
@SerialName("paid_at") val paidAt: String,
val total: Double,
val coupon: JsonElement?,
val customer: Customer,
val items: List<Item>,
)
@Serializable
data class Customer(
val name: String,
val email: String,
val vip: Boolean,
)
@Serializable
data class Item(
val sku: String,
val qty: Int,
val price: Double,
val tags: List<String>? = null,
)How each JSON value is typed
| In the JSON | In Kotlin |
|---|---|
| string | String |
| integer | Int |
| decimal | Double |
| true / false | Boolean |
| null | JsonElement? |
| array of items | List<Item> |
| field missing in some items | List<String>? = null |
The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.
Awkward keys, null and empty lists
{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Account(
@SerialName("user_id") val userId: Int,
@SerialName("display name") val displayName: String,
val tags: List<JsonElement>,
val address: JsonElement?,
val scores: List<Double>,
val meta: Meta,
val mixed: List<JsonElement>,
)
@Serializable
data class Meta(
@SerialName("2fa") val field2fa: Boolean,
)"user_id" becomes userId with @SerialName("user_id"), "display name" becomes displayName, "2fa" becomes field2fa, and null and the empty list become JsonElement? and List<JsonElement>, which keep whatever arrives without guessing its type.
The other flavour
The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "gson" one:
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Order(
val id: Int,
@SerialName("paid_at") val paidAt: String,
val coupon: JsonElement?,
)import com.google.gson.annotations.SerializedName
data class Order(
val id: Int,
@SerializedName("paid_at") val paidAt: String,
val coupon: Any?,
)The Gson flavour uses @SerializedName and plain classes for Android projects that already use Gson.
Decoding with kotlinx.serialization
val order = Json.decodeFromString<Root>(text)
println(order.customer.name)
// Json { ignoreUnknownKeys = true } to accept new fieldsYou need the kotlinx-serialization-json dependency and the serialization compiler plugin. By default unknown keys are an error; build the decoder with ignoreUnknownKeys = true if the API adds fields over time.
Common mistakes
- Forgetting @Serializable on a nested class: the compiler fails with "serializer has not been found". The tool puts it on every class it writes.
- Non-null types for fields the API sometimes omits: decoding then throws MissingFieldException. Make them nullable with a default.
How to use the converter
- Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
- Set the root name and the flavour; the code updates as you type.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert JSON to a Kotlin data class?
Paste the JSON, choose kotlinx or Gson and copy the classes. With kotlinx call Json.decodeFromString.
kotlinx.serialization or Gson?
kotlinx.serialization is checked at compile time, supports Kotlin multiplatform and respects non-null types; Gson uses reflection and ignores them. Prefer kotlinx for new code.
Can I use it with Retrofit or Ktor?
Yes: both read these classes through their kotlinx.serialization or Gson converters.