Tool Kumar tool
JSON to Kotlin data classes
Turn example JSON into data classes for kotlinx.serialization. Nested objects get their own classes, arrays become List values and renamed fields keep their wire names through @SerialName.
How to use this tool
- Paste a representative response, including records whose fields differ.
- Review the generated output and change the root name if needed.
- Copy or download the code, then check the types against your API contract.
Worked example
This output comes from the built-in converter. The two item prices infer a floating-point field; the null and string notes infer a nullable field.
Input
{
"user-id": 42,
"profile": {
"city": "London",
"created_at": "2026-10-14T09:00:00Z"
},
"items": [
{
"price": 12,
"note": null
},
{
"price": 12.5,
"note": "gift"
}
]
}Output
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Root(
@SerialName("user-id") val userId: Long,
val profile: Profile,
val items: List<Item>,
)
@Serializable
data class Profile(
val city: String,
@SerialName("created_at") val createdAt: String,
)
@Serializable
data class Item(
val price: Double,
val note: String? = null,
)
Decode an Android API response
Enable the Kotlin serialization compiler plugin and add the kotlinx.serialization JSON dependency in your app. Use the generated Root type with Json.decodeFromString.
ignoreUnknownKeys allows new server fields without failing. Set it only when your API policy allows unknown fields.
import kotlinx.serialization.json.Json
val json = Json { ignoreUnknownKeys = true }
val model = json.decodeFromString<Root>(responseBody)Options and edge cases
- The generator emits @Serializable on each data class and imports kotlinx.serialization annotations. It does not emit Moshi @Json or Gson @SerializedName.
- Integers use Long, fractions use Double and null-only or mixed values use JsonElement. Nullable fields default to null.
- Integers beyond signed 64-bit range become Double, which can lose precision. Use strings or a custom serializer for exact identifiers.
- Date strings remain String. A single sample cannot tell whether a field is required in every API response.
Private by default
Your document stays in this browser. Tool Kumar parses, converts and creates downloads on your device, with no account or upload step.
JSON to Kotlin data classes questions
- Which Kotlin serialization library does it use?
- kotlinx.serialization. The output uses @Serializable and adds @SerialName when a Kotlin property differs from its JSON key.
- Can I use it with Moshi?
- The field types can help you start a model, but the annotations target kotlinx.serialization. Replace them and configure Moshi code generation if your Android app uses Moshi.
- How are missing and null fields handled?
- Fields missing from some array records or observed as null become nullable and default to null. Required non-null fields have no default.
- Does it create Kotlin DTOs for Android?
- Yes. It creates data classes suitable as DTOs. Configure the Kotlin serialization compiler plugin and the JSON runtime in your project.