JSON to Kotlin Data Class
Paste JSON and get Kotlin data classes for every nested object, with nullable properties where your sample shows missing or null values. Free, no signup, and generated in your browser.
16 lines · processed in your browser, nothing is uploaded
Kotlin output · Root.kt
Generated code appears here.Example: JSON to Kotlin
This JSON sample, with the root type named User, produces the Kotlin below using the default options.
{
"id": 1024,
"name": "Ada Lovelace",
"email": "ada@example.com",
"active": true,
"score": 98.5,
"tags": [
"admin",
"beta"
],
"address": {
"city": "London",
"postcode": "NW1 6XE"
}
}// To parse the JSON, install kotlin's serialization plugin and do:
//
// val json = Json { allowStructuredMapKeys = true }
// val user = json.parse(User.serializer(), jsonString)
package com.example
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
@Serializable
data class User (
val id: Long,
val name: String,
val email: String,
val active: Boolean,
val score: Double,
val tags: List<String>,
val address: Address
)
@Serializable
data class Address (
val city: String,
val postcode: String
)
How to convert JSON to Kotlin
- 1Paste a JSON object or array into the input.
- 2Select Kotlin as the output language.
- 3Name the root class, for example Profile.
- 4Adjust options such as the serialization framework if available.
- 5Copy the data classes into a .kt file.
kotlinx.serialization or Moshi?
kotlinx.serialization is JetBrains' official library: annotate a class with @Serializable, apply the compiler plugin, and decode with Json.decodeFromString<Profile>(text). It works in Kotlin Multiplatform projects, not just on the JVM.
Moshi is popular on Android alongside Retrofit. Use @JsonClass(generateAdapter = true) with codegen so it understands Kotlin nullability and default values instead of relying on reflection.
Renaming keys
To keep camelCase property names for snake_case keys, use @SerialName("user_id") with kotlinx.serialization or @Json(name = "user_id") with Moshi.
kotlinx.serialization throws on unknown keys by default; create the decoder with Json { ignoreUnknownKeys = true } if the API may add fields.
Nullability and defaults
A property typed String? accepts null, but a missing key still fails unless the property has a default value. Give optional properties a default such as = null.
Paste several records so the generator can see which fields are sometimes absent.