Skip to main content

JSON to Kotlin Data Class

Convert JSON to Kotlin data classes for kotlinx.serialization, Gson, Moshi or Jackson, with nullable types, defaults and @SerialName annotations.

100% client-side · no data leaves your browser
Kotlin — 3 types
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

@Serializable
data class Root(
    val id: Int,
    val username: String,
    val email: String,
    val isActive: Boolean,
    val rating: Double,
    val createdAt: String,
    val avatarUrl: JsonElement? = null,
    val tags: List<String>,
    val address: Address,
    val orders: List<Order>,
)

@Serializable
data class Address(
    val street: String,
    val city: String,
    val zipCode: String,
)

@Serializable
data class Order(
    val orderId: String,
    val total: Double,
    val items: Int,
    val shipped: Boolean,
    val coupon: String? = null,
)

Arrays of objects are merged: a key missing from some items becomes optional, null makes a field nullable, and identical shapes share one type. Paste several examples in an array for the most accurate types.

About this tool

Paste JSON and get Kotlin data classes for your favourite JSON library. Choose kotlinx.serialization (@Serializable and @SerialName), Gson (@SerializedName), Moshi with code generation (@JsonClass and @Json) or Jackson (@JsonProperty), or plain data classes. Properties are camelCase, annotations are added where a key differs, keywords are escaped with backticks, and optional or null values become nullable with a null default. Int or Long is chosen from the values, decimals become Double, lists become List<T> and ID-keyed objects become Map<String, T>. Nested objects get their own data classes. Switch to Go, Rust, C# or Python from the same page.

How to convert JSON to Kotlin data classes

Generate Kotlin data classes from JSON.

  1. 1

    Paste JSON

    Paste a JSON object, or an array of examples.

  2. 2

    Pick a library

    Choose kotlinx.serialization, Gson, Moshi, Jackson or plain classes, and a package.

  3. 3

    Copy the code

    Copy the data classes or download Types.kt.