JSON to Kotlin Converter Online — Generate Data Class

Generate Kotlin data class definitions from any JSON payload — immutable val properties, null-safe types, nested data classes, and kotlinx.serialization compatibility. 100% in your browser.

What is a JSON to Kotlin converter?

A JSON to Kotlin converter reads a JSON sample and emits typed data class definitions that mirror the JSON shape — immutable val properties, null-safe types, nested data classes, and kotlinx.serialization-friendly naming. The output drops straight into an Android, Ktor, Spring Boot for Kotlin, or Compose Multiplatform project and deserializes with kotlinx.serialization, Moshi, or Gson without manual wiring.

Writing data classes by hand from a 200-field API payload wastes hours and is bug-prone — a single typo in a property name silently nulls out a value at runtime. OpenFormatter's converter walks the JSON tree once, generates idiomatic Kotlin, and runs entirely in your browser so internal API responses never leave your machine.

How to generate Kotlin data classes from JSON — 4 steps

  1. Paste a real JSON response. Use a representative sample — type inference quality depends on it. Booleans must be true/false (not 0/1), numbers must be unquoted.
  2. Click Convert. The tool emits a Root data class plus one separate data class per nested object, with val properties.
  3. Copy and paste into your IDE. Each data class is independent — split them into separate .kt files in your com.example.model package.
  4. Deserialize. Add @Serializable and call Json.decodeFromString<Root>(json) — that is all kotlinx.serialization needs once the data class matches the JSON shape.

JSON to Kotlin type mapping

Sample JSON

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "score": 98.5,
  "address": { "city": "London" },
  "tags": ["admin", "engineer"]
}

Generated Kotlin data class

import kotlinx.serialization.Serializable

@Serializable
data class Root(
    val id: Int,
    val name: String,
    val active: Boolean,
    val score: Double,
    val address: Address,
    val tags: List<String>
)

@Serializable
data class Address(
    val city: String
)

Idiomatic data class

Immutable val properties, primary constructor only, automatic equals/hashCode/toString/copy — exactly what kotlinx.serialization expects to round-trip JSON safely.

Null safety baked in

Non-null Kotlin types for concrete sample values. JSON null values produce Any? — refine to Type? when the field is sometimes absent in the wire format.

Client-Side Only

JSON is parsed in JavaScript inside the browser. Internal Android API responses, tokens, or PII never reach a server.

Common use cases

  • check_circleGenerating Android Retrofit response bodies from a JSON spec
  • check_circleBuilding Ktor server route DTOs from a frontend JSON payload
  • check_circleProducing Spring Boot for Kotlin @RestController request and response classes
  • check_circleGenerating Compose Multiplatform shared data models
  • check_circleModelling Firebase Realtime Database snapshot structures
  • check_circleBuilding Jetpack Room database entities from a JSON export
  • check_circleGenerating GraphQL response shapes for a Kotlin app
  • check_circleModelling Stripe, Slack, GitHub webhook event payloads as @Serializable data classes

Why client-side generation matters

The JSON you paste often contains real customer data, OAuth tokens, signed URLs, or internal field names you do not want indexed by a third-party converter. OpenFormatter generates the Kotlin entirely in JavaScript on your device — open the Network tab in DevTools and you will see zero requests when you click Convert. Safe for enterprise use, safe behind a corporate proxy, safe when the API contract is itself confidential.

Need other Kotlin tooling?

Format Java/Kotlin source, convert XML to Kotlin, or generate types for other JVM languages — all browser-side.

Frequently Asked Questions

data class with val or var?

The generator emits val (read-only) properties by default because that is the idiomatic Kotlin choice for DTOs — immutable models prevent accidental mutation, work safely across threads, and make the data class .copy() function meaningful. If you need mutable models (e.g. for ViewBinding two-way binding) change val to var manually after copying. data class equality and hashCode work identically with val or var.

Does it support kotlinx.serialization?

Yes. The generated data class is compatible — add @Serializable above each class and Json.decodeFromString<Root>(jsonString) works directly. For JSON keys that do not match Kotlin property names use @SerialName("original_key") on the property. Make sure org.jetbrains.kotlinx.serialization plugin is applied in your build.gradle.kts.

How do I deserialize the generated data class?

With kotlinx.serialization: val root = Json.decodeFromString<Root>(jsonString). With Moshi: val adapter = moshi.adapter(Root::class.java); val root = adapter.fromJson(jsonString). With Retrofit: declare suspend fun fetch(): Root and let the converter factory handle it. All three work because the data class has a primary constructor matching the JSON keys.

How are nullable types handled?

JSON null in the sample produces Any? on the Kotlin side; concrete non-null values produce non-nullable types like String, Int, Boolean, Double. To express an optional field, change String to String? after copying — kotlinx.serialization then accepts the field being absent in the JSON if you also add a default value (val name: String? = null).

How are nested JSON objects modelled?

Each nested object becomes its own top-level Kotlin data class so you can split them into separate .kt files. The parent data class holds a typed reference (val address: Address) and kotlinx.serialization or Moshi recurse automatically when both classes are annotated.

How are JSON arrays of objects mapped?

Arrays of objects become List<DataClassName>. Arrays of primitives become List<String>, List<Int>, List<Double>, or List<Boolean>. kotlinx.serialization and Moshi both handle Kotlin List<T> with no extra adapters required.

Can I use this with Android and Retrofit?

Yes. Drop the data classes into your Android project and declare a Retrofit interface like @GET("users/{id}") suspend fun getUser(@Path("id") id: Int): User. Wire either a Moshi or kotlinx.serialization converter factory in your Retrofit.Builder and Retrofit deserializes responses directly into the data class.

Is the JSON uploaded to your servers?

No. Conversion runs entirely in your browser via JavaScript. Open DevTools → Network and click Convert — no requests are made. JSON containing API keys, internal field names, or PII never leaves your machine.

JSON to Kotlin Converter — Generate Data Class