JSON to Kotlin Data Class Generator

Paste JSON and get Kotlin data classes with nested types, List<T>, nullable fields and kotlinx.serialization, Gson or Moshi annotations. Runs in your browser, so your data is never uploaded.

JSON Input

Ln 1, Col 1Empty

Kotlin

Your result will appear here.
No result yet

What Is a JSON to Kotlin Converter?

A JSON to Kotlin converter reads example JSON, like a response from your backend, and writes Kotlin data classes that match it: the right types, a class for every nested object, lists for arrays and nullable types for values that can be missing. It is the quickest way to get model classes for an Android app or a Ktor service without typing every property by hand.

How to Convert JSON to Kotlin Data Classes

  1. Paste a sample of your JSON, or drop a .json file on the box.
  2. Choose a class name and the library your project uses: kotlinx.serialization, Gson or Moshi.
  3. Click Generate Kotlin, or press Ctrl + Enter.
  4. Copy the code, or download it as a .kt file.

Example

JSON
{
  "id": 4821,
  "user_name": "emily_clark",
  "display_name": "Emily Clark",
  "verified": true,
  "rating": 4.8,
  "avatar_url": null,
  "settings": {
    "dark_mode": true,
    "language": "en"
  },
  "followers": [
    {
      "id": 17,
      "user_name": "james_s"
    },
    {
      "id": 23,
      "user_name": "olivia_b",
      "mutual": true
    }
  ]
}
Kotlin (kotlinx.serialization)
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

@Serializable
data class Profile(
    val id: Long,
    @SerialName("user_name")
    val userName: String,
    @SerialName("display_name")
    val displayName: String,
    val verified: Boolean,
    val rating: Double,
    @SerialName("avatar_url")
    val avatarUrl: JsonElement? = null,
    val settings: Settings,
    val followers: List<Follower>,
)

@Serializable
data class Settings(
    @SerialName("dark_mode")
    val darkMode: Boolean,
    val language: String,
)

@Serializable
data class Follower(
    val id: Long,
    @SerialName("user_name")
    val userName: String,
    val mutual: Boolean? = null,
)

Snake_case keys like user_name become camelCase properties (userName) with @SerialName keeping the original key. The followers array becomes List<Follower>, and mutual is Boolean? = null because only one follower has it. avatar_url is always null in the sample, so its real type is unknown: change JsonElement? to String? if it holds a URL.

How the Types Are Chosen

JSON valueKotlin type
TextString
Whole numberLong, so IDs above 2,147,483,647 never overflow. Change it to Int if your values are small.
Number with decimalsDouble
true / falseBoolean
ObjectIts own data class, like Settings
ArrayList<T>, like List<Follower>
Missing in some items, or nullA nullable type with a default, like String? = null
Only null, or mixed typesJsonElement? with kotlinx.serialization, Any? otherwise. Replace it with the real type if you know it.

Kotlin keywords used as keys, like class or in, are written in backticks (val `class`: String), so the code still compiles.

Decode JSON in Kotlin

kotlinx.serialization
import kotlinx.serialization.json.Json

val json = Json { ignoreUnknownKeys = true }
val profile = json.decodeFromString(Profile.serializer(), text)

println(profile.settings.language)   // en

kotlinx.serialization needs the kotlin("plugin.serialization") Gradle plugin and the kotlinx-serialization-json library. By default it throws an error when the JSON has a key your class does not, so ignoreUnknownKeys = true is a good idea for API responses that may grow.

Gson
import com.google.gson.Gson

val profile = Gson().fromJson(text, Profile::class.java)
Moshi
import com.squareup.moshi.Moshi

val moshi = Moshi.Builder().build()
val profile = moshi.adapter(Profile::class.java).fromJson(text)

The @JsonClass(generateAdapter = true) annotation tells Moshi's code generator (moshi-kotlin-codegen, run with KSP) to write a fast adapter for each class. If the JSON is an array at the top level, decode it as a List of the item class.

Which Library Should You Pick?

  • kotlinx.serialization is made by JetBrains, works on Android, the JVM and Kotlin Multiplatform, and respects Kotlin's null safety and default values. A good default for new projects.
  • Moshi is popular in Android apps that use Retrofit, and its code generator also understands nullable types and defaults.
  • Gson is common in older Android projects. It creates objects without calling the constructor, so default values are ignored and a missing key can leave null in a property you declared as non-null. Check those fields, or prefer one of the other two for new code.

Tips

  • Dates: date strings stay String. Parse ISO 8601 values like 2026-11-18T09:30:00Z with java.time.Instant.parse(), or use a date serializer for your library.
  • Use a complete sample. The generator only sees the fields in your example. Include an item that has every optional field if you can.
  • Check the JSON first. Broken JSON makes every library throw. Fix it with JSON Repair or find the exact error in the JSON Validator.

Your JSON never leaves your browser, so it is safe to paste real API responses. Need another language? Try JSON to Java, JSON to TypeScript or JSON to Go. For iOS and Flutter apps, use JSON to Swift or JSON to Dart.

Frequently Asked Questions

How do I convert JSON to a Kotlin data class?

Paste a sample of your JSON into the converter and click Generate Kotlin. It writes data classes with nested types, List<T> and nullable fields, plus kotlinx.serialization, Gson or Moshi annotations.

Which library should I use: kotlinx.serialization, Gson or Moshi?

kotlinx.serialization is a good default for new projects and works on Kotlin Multiplatform. Moshi is popular with Retrofit on Android. Gson is common in older projects but ignores Kotlin default values and null safety.

Why are whole numbers Long instead of Int?

Long holds IDs larger than 2,147,483,647 without overflowing. If you know your values are small, change the type to Int.

Is my JSON uploaded?

No. The Kotlin code is generated in your browser and your JSON never leaves your device.

Related Tools