JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for Android, with kotlinx.serialization, Gson or Moshi names, nullable fields and defaults. Free, in your browser.

Runs in your browser. Your data never leaves your device.

How to use the JSON to Kotlin Data Class

  1. Paste a sample JSON response, or an array of items, into the box.
  2. Choose the library (kotlinx.serialization, Gson, Moshi or none), the root class name and a package if you want one.
  3. Tick the options you need: all fields nullable, default values, or var instead of val.
  4. Copy the classes into a .kt file in your project.

About this tool

Paste a JSON response from your API and this converter writes the Kotlin data classes for it. Nested objects become their own classes, arrays become List types, and snake_case keys become camelCase properties with the original name kept in @SerialName, @SerializedName or @Json. A field that is null, or missing from some items of an array, becomes nullable, so the model doesn't crash on real data.

How to use JSON in Kotlin

On Android the usual way today is kotlinx.serialization. Add the Kotlin serialization Gradle plugin and the kotlinx-serialization-json library, mark your classes with @Serializable (the converter adds it when you choose kotlinx.serialization), then read and write JSON with a Json instance. ignoreUnknownKeys = true keeps the app working when the server adds new fields.

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

@Serializable
data class User(val id: Int, val name: String, val email: String? = null)

val json = Json { ignoreUnknownKeys = true }

// JSON to Kotlin
val user = json.decodeFromString(User.serializer(), """{"id":1,"name":"Sara"}""")

// Kotlin to JSON
val text = json.encodeToString(User.serializer(), user)   // {"id":1,"name":"Sara"}

Kotlin to JSON

Going the other way, from a Kotlin object to a JSON string, uses the same data class: encodeToString in kotlinx.serialization, toJson() in Gson, or adapter.toJson() in Moshi. kotlinx.serialization leaves out properties that still have their default value unless you set encodeDefaults = true.

Data classes with Gson and Moshi

A data class gives you equals(), hashCode(), toString() and copy() for free, which is what a model needs. With Retrofit, use the converter that matches your project: kotlinx.serialization for new Kotlin and Compose apps, Gson or Moshi when the project already uses them. Gson creates objects without calling the constructor, so it skips Kotlin's default values and can put null into a field declared as String; making such fields nullable avoids that crash.

The Android Studio plugin and other languages

Android Studio and IntelliJ IDEA have a JSON To Kotlin Class plugin that does the same job inside the IDE. This page needs no install and also works on a phone or a computer that isn't yours. Need the same model in another language? The JSON tools below convert to Java, Swift, Dart, TypeScript, Go, C# and Python. C++ has no class step: the nlohmann/json library reads JSON straight into its own json type.

GuideTic Tac Toe in Android Studio (Kotlin): Full Source Code with an Unbeatable AI

Frequently asked questions

How do I convert JSON to a data class in Kotlin?

Paste the JSON into the box above, choose your library and copy the result. The converter reads every key, picks the type (Int, Long, Double, Boolean, String, a nested class or a List) and writes one data class per object.

How can I use JSON in Kotlin?

Use kotlinx.serialization: add the plugin and library, mark the data class @Serializable, then call decodeFromString to read JSON and encodeToString to write it. Gson and Moshi work too, mostly in older Android projects.

Is Kotlin just better than Java?

For Android, mostly yes: Google has recommended Kotlin first since 2019, and Jetpack Compose is Kotlin only. Kotlin needs less code, has null safety in its type system and coroutines for background work. Java still runs on the same JVM and both can live in one project, so moving over can be gradual.

Which IDE is best for Kotlin?

Android Studio for Android apps, and IntelliJ IDEA for server, desktop or Multiplatform Kotlin. Both are built on JetBrains' Kotlin support and have the best code completion and refactoring. VS Code can edit Kotlin with a language server extension, but with fewer features.

Which library should I choose?

kotlinx.serialization is the modern choice for new Kotlin and Compose apps. Choose Gson or Moshi to match an existing Retrofit setup.

Is my JSON sent anywhere?

No. The classes are made in your browser and your JSON never leaves your device.

More json tools

JSON and code models

All 31 free tools →
JSON tool · Runs in your browser

JSON Formatter

Format JSON online: beautify messy JSON with 2 or 4 spaces or tabs, sort keys, minify it to one line and find errors. Free, runs in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON Validator

Check if JSON is valid and see the exact line and column of any error, with a hint on how to fix it. A free online JSON validator in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Dart (Flutter)

Convert JSON to null-safe Dart model classes for Flutter, with fromJson, toJson and an optional copyWith. Free, no sign-up, runs in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Swift (Codable)

Turn JSON into Swift Codable structs or classes, with CodingKeys for snake_case keys. Free JSON to Swift converter for iOS and macOS apps.

Free · No sign-upOpen tool