JSON to Kotlin

Kotlin data classes for kotlinx.serialization, Gson or Moshi.

About this tool

Paste JSON, pick kotlinx.serialization, Gson or Moshi, optionally add a package, and copy the generated data classes into your Android or Kotlin project.

How the Kotlin is generated

Each JSON object becomes a data class and each key a camelCase val (user_name โ†’ userName). A @SerialName, @SerializedName or @Json(name) annotation is added only where the JSON key differs. Nested objects become their own data classes, arrays List<T>, whole numbers Long, decimals Double and booleans Boolean. Array items are merged before types are chosen, so a key that only some items have, or that is sometimes null, is treated as optional or nullable rather than breaking decoding. Those become nullable with a null default (val coupon: String? = null), so missing keys don't throw. Kotlin keywords are escaped with backticks. Honest limits: types describe your sample. Always-null values become JsonElement (kotlinx) or Any, and with kotlinx you may want ignoreUnknownKeys for evolving APIs. JSON to Java, JSON to Swift and the JSON formatter are related.

Frequently asked questions

How do I convert JSON to a Kotlin data class?

Paste the JSON here, choose your serialization library and copy the data classes.

Which library should I use?

kotlinx.serialization is Kotlin-first and multiplatform. Gson and Moshi are common in older Android projects.

Why do some fields have = null?

Keys that are missing or null in some items get a null default so decoding succeeds.

Is my JSON uploaded?

No. Everything runs in your browser and nothing is stored or sent.