"category": "code-generator",

JSON to Kotlin Data Class Generator

"tldr": Paste JSON and get Kotlin data classes annotated for kotlinx.serialization - @SerialName added wherever names differ.

Convert JSON into Kotlin data classes annotated for kotlinx.serialization. Properties are camelCased with @SerialName preserving the original keys, nested objects become their own data classes, and arrays become List<T>.

{"json": "kotlin data class"}

This is the standard shape for Android and Ktor projects: immutable val properties, value equality, and copy() for free. The same classes work with Moshi or Gson by swapping the annotations.

How to convert JSON to Kotlin data classes

  1. 1Paste a representative JSON payload into the input panel.
  2. 2Properties whose JSON key differs from the Kotlin name get a @SerialName annotation automatically.
  3. 3Nested objects become separate data classes; arrays become List<T>.
  4. 4Copy the classes into your project and decode with Json.decodeFromString.

Convert JSON to Kotlin data class in code

Decode with kotlinx.serialization
import kotlinx.serialization.json.Json

val json = Json { ignoreUnknownKeys = true }
val order = json.decodeFromString<Root>(text)
println(order.customer.name)
Same classes with Moshi
@JsonClass(generateAdapter = true)
data class Root(
    @Json(name = "order_id") val orderId: Long,
    val total: Double,
)

Frequently asked questions

kotlinx.serialization, Moshi, or Gson - which should I use?

kotlinx.serialization is the Kotlin-first choice (compile-time, multiplatform, no reflection). Moshi is excellent on Android with codegen. Gson is legacy but ubiquitous. The generated classes convert between them by swapping @SerialName for @Json(name=...) (Moshi) or @SerializedName (Gson).

How do I handle fields that may be missing?

Give them default values: val notes: String? = null. With kotlinx.serialization, a property without a default is required and decoding fails if the key is absent - defaults are how you express optionality.

Why Long and Double instead of Int and Float?

JSON numbers are unbounded; IDs and timestamps overflow Int (2.1 billion max) surprisingly often. Long and Double are safe defaults - narrow deliberately when you know the range.

Do I need @Serializable on every class?

Yes - kotlinx.serialization generates serializers at compile time only for annotated classes, including every nested class. Forgetting one produces a "Serializer for class ... not found" error at runtime.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →