Youtilities

Popular

All tools

JSON to Kotlin Data Class

Generate Kotlin data classes from JSON, for kotlinx.serialization, Gson, or plain Kotlin.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

Kotlin
Kotlin types appear here.

What this does

Paste JSON and get Kotlin data classes for it. Pick kotlinx.serialization (@Serializable), Gson (@SerializedName), or plain classes with no annotations.

Nullable and optional

A key that is missing from some items, or sometimes null, becomes a nullable type with a default of null, like val note: String? = null. Whole numbers are Long, decimals Double.

Names

Properties are camelCase. When that differs from the JSON key (first-name → firstName) the original key is kept in a @SerialName or @SerializedName annotation. Two objects with the same shape share one class.

Using the kotlinx output

Apply the serialization plugin in Gradle, kotlin("plugin.serialization"), and add the kotlinx-serialization-json library. Then decode with Json.decodeFromString<Root>(text). By default kotlinx throws when the JSON has a key the class does not declare, so if the API may add fields, use Json { ignoreUnknownKeys = true }. A key missing from the JSON is fine only if the property has a default, which is why the tool gives every nullable or sometimes-missing property = null. Values with no single type become JsonElement, since kotlinx cannot decode Any.

Gson and plain classes

With Gson, @SerializedName keeps the original key. Be aware that Gson can create objects without calling the Kotlin constructor, so a property declared non-null may still end up null at runtime when its key is missing, and constructor defaults may be skipped. If your data is irregular, turn on All optional so every property is nullable. Plain classes have no annotations, so property names must match the JSON keys or be mapped by whatever library you use. Kotlin keywords used as keys, such as object or in, are kept with backticks.

All json tools