DevToolsmith

JSON to Kotlin Data Class Converter

Paste JSON to generate Kotlin data classes for kotlinx.serialization, Moshi, Jackson or Gson. Keys become camelCase properties with the right annotation, and nullable or missing values get a null default.

Runs 100% in your browser — nothing is uploaded

JSON
Tip: drop a file here361 chars · 361 bytes · 14 lines
Kotlin data classes
593 chars · 593 bytes · 30 lines

Tip: paste an array of several objects — keys missing from some of them become optional (? = null).

Example

JSON input
[
  {
    "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"
    }
  }
]
Generated Kotlin (root name: User)
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_name became fullName with @SerialName("full_name").
  • avatar_url is null for one user, so it is String?.
  • team only 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

LibraryClass annotationRename a key
kotlinx.serialization@Serializable@SerialName("key")
Moshi (codegen)@JsonClass(generateAdapter = true)@Json(name = "key")
Jacksonnone (needs jackson-module-kotlin)@JsonProperty("key")
Gsonnone@SerializedName("key")

Type mapping

JSONKotlin
stringString
integerLong
decimal numberDouble
true / falseBoolean
objectdata class
arrayList<T>
sometimes missing or nullT? = null
always null / mixed typesJsonElement (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.