Example
[
{
"id": 1,
"full_name": "Ada Lovelace",
"avatar_url": "https://example.com/a.png",
"is_admin": true
},
{
"id": 2,
"full_name": "Grace Hopper",
"avatar_url": null,
"is_admin": false,
"team": {
"id": 7,
"name": "Compilers"
}
}
]
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
// The JSON root is an array: deserialize it as List<UserItem>
@Serializable
data class UserItem(
val id: Long,
@SerialName("full_name") val fullName: String,
@SerialName("avatar_url") val avatarUrl: String? = null,
@SerialName("is_admin") val isAdmin: Boolean,
val team: Team? = null,
)
@Serializable
data class Team(
val id: Long,
val name: String,
)
full_namebecamefullNamewith@SerialName("full_name").avatar_urlis null for one user, so it isString?.teamonly appears for one user, so it is optional:Team? = null.
Decoding with kotlinx.serialization
import kotlinx.serialization.json.Json
val json = Json { ignoreUnknownKeys = true }
val users: List<UserItem> = json.decodeFromString(text)
println(users.first().fullName)
val text2 = json.encodeToString(users)
The @Serializable annotation needs the serialization compiler plugin:
plugin.serialization in your Gradle plugins block, next to the Kotlin plugin of the same version.
Annotations by library
| Library | Class annotation | Rename a key |
|---|---|---|
| kotlinx.serialization | @Serializable | @SerialName("key") |
| Moshi (codegen) | @JsonClass(generateAdapter = true) | @Json(name = "key") |
| Jackson | none (needs jackson-module-kotlin) | @JsonProperty("key") |
| Gson | none | @SerializedName("key") |
Type mapping
| JSON | Kotlin |
|---|---|
| string | String |
| integer | Long |
| decimal number | Double |
| true / false | Boolean |
| object | data class |
| array | List<T> |
| sometimes missing or null | T? = null |
| always null / mixed types | JsonElement (kotlinx) or Any |
Coming from Java? The JSON to Java converter generates records and POJOs for the same JSON.
Frequently asked questions
How do I convert JSON to a Kotlin data class?
Paste a JSON sample into the tool above and pick your serialization library. It creates a data class for every object, converts keys to camelCase property names with the right annotation for the original key, and marks properties that can be null or missing as nullable with a null default.
Which library should I choose?
kotlinx.serialization is the official Kotlin library, works on every Kotlin platform including Multiplatform, and checks types at compile time. Moshi is popular on Android with Retrofit. Jackson fits Spring Boot projects. Gson works, but it ignores Kotlin null safety and default values, so new projects should prefer one of the others.
Why do nullable properties have = null?
kotlinx.serialization and Moshi treat a property without a default as required, and throw an error when the key is missing from the JSON, even if the type is nullable. The = null default makes the key optional. Keys that appear in every sample stay required.
Why does decoding fail with “Encountered an unknown key”?
kotlinx.serialization rejects JSON keys that have no matching property by default. APIs often add fields over time, so create your Json instance with ignoreUnknownKeys = true, as in the example on this page.