How it works
Paste a JSON sample and get Kotlin data classes:
- Every object becomes a data class named after its property (
customer→Customer,items→Item). The root isRootunless you rename it. - Properties are lowerCamelCase
vals. When the name differs from the key, the key is kept with@SerialName. - Arrays of objects are merged, so a key that only some items have becomes a nullable property.
- Nullable and optional values get
?; lists areList<T>, empty objectsMap<String, JsonElement>. - Each property ends with a trailing comma (Kotlin 1.4+), so adding fields gives clean diffs.
Example
{
"user_id": 42,
"name": "Linh",
"tags": ["admin"],
"addresses": [
{ "city": "Hanoi", "zip": "100000", "note": null },
{ "city": "HCMC", "zip": "700000", "primary": true, "note": "Gate 2" }
]
}
becomes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Root(
@SerialName("user_id") val userId: Int,
val name: String,
val tags: List<String>,
val addresses: List<Address>,
)
@Serializable
data class Address(
val city: String,
val zip: String,
val note: String? = null,
val primary: Boolean? = null,
)
Options
- Root name: name of the top-level class.
- kotlinx.serialization: add
@Serializable/@SerialNameand useJsonElementfor unknown values. Off: plain classes withAny?. - = null defaults: give every nullable property a
nulldefault.
Reserved words and odd keys
Hard keywords are wrapped in backticks, which keeps the property name equal to the key, so no @SerialName is needed. Other keys are converted. For {"class": "vip", "default": null, "2fa": true, "first name": "Linh", "created_at": 1727712000000} the class is:
@Serializable
data class Root(
val `class`: String,
val default: JsonElement? = null,
@SerialName("2fa") val field2fa: Boolean,
@SerialName("first name") val firstName: String,
@SerialName("created_at") val createdAt: Long,
)
default is only a soft keyword in Kotlin, so it stays as is. Classes that would shadow String, List, Map or Any get a Type suffix. A root array or primitive becomes a typealias, for example typealias Root = List<RootItem>.
Decoding
val json = Json { ignoreUnknownKeys = true }
val root = json.decodeFromString<Root>(text)
ignoreUnknownKeys stops decoding from failing when the API adds a field. Without it you get “Encountered an unknown key”.
Using Gson, Moshi or Jackson instead
Turn kotlinx.serialization off to get annotation-free classes. Unknown values become Any?, and every renamed property carries its key in a comment, for example val userId: Int, // JSON key: "user_id". Replace the comment with @SerializedName("user_id") for Gson, @Json(name = "user_id") for Moshi, or @JsonProperty("user_id") for Jackson with the Kotlin module.
Common mistakes
- Generating from a single object. Every key becomes non-null. Paste several real responses so optional keys are detected.
- Missing the plugin.
@Serializableneeds theorg.jetbrains.kotlin.plugin.serializationGradle plugin. Without it you get “Serializer for class ‘Root’ is not found”. - Whole-number prices.
25becomesInt; a later25.5fails to decode. Use a sample with decimals or change the type toDouble.
Other targets: JSON to Java, JSON to Swift for iOS, and JSON to TypeScript for the web client.
Frequently asked questions
Which serialization library does the output target?
With the kotlinx.serialization option on (the default), classes get `@Serializable` and renamed keys get `@SerialName`, ready for `Json.decodeFromString<Root>(json)`. With it off you get plain data classes; renamed keys are noted in a `// JSON key:` comment so you can add Gson's `@SerializedName` or Moshi's `@Json(name = ...)` yourself.
Why do nullable properties default to `= null`?
kotlinx.serialization throws `MissingFieldException` when a key is absent and the property has no default. A `= null` default lets optional keys be missing. Turn the option off if you want every key to be required.
Why is a property typed JsonElement?
The sample had no single type for it: every value was null, the array was empty, or the values mixed types. kotlinx.serialization cannot serialize `Any`, so `JsonElement` holds any JSON value. In plain mode the type is `Any?`.
How are numbers typed?
`Int` when every value fits in 32 bits, `Long` up to 2^63−1, and `Double` when any value has a fraction. Integers beyond `Long` become `java.math.BigInteger` in plain mode; with kotlinx.serialization they become `JsonPrimitive` with a warning, because the library has no built-in BigInteger serializer.
Is my JSON uploaded?
No. The data classes are generated entirely in your browser; nothing is sent to a server.
Last updated