JSON to Kotlin

Input
Output

JSON to Kotlin

Paste a JSON sample, such as an API response, and Bracely writes Kotlin data classes for kotlinx.serialization for it. Each object becomes a @Serializable data class, with @SerialName where a key needs it.

Example: a user profile for an Android app

Two profiles from a mobile backend. The key with a dash gets @SerialName with a camelCase property, fields missing from one profile become nullable with a null default, and 4.5 and 3 together make the score a Double:

Input

[
  {
    "id": 1,
    "display-name": "Ananya",
    "email": "[email protected]",
    "rating": 4.5,
    "badges": [
      "early-adopter"
    ],
    "settings": {
      "dark_mode": true,
      "locale": "en-IN"
    }
  },
  {
    "id": 2,
    "display-name": "Kabir",
    "rating": 3,
    "settings": {
      "dark_mode": false,
      "locale": "hi-IN"
    }
  }
]

Output

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Profile(
    val id: Long,
    @SerialName("display-name") val displayName: String,
    val email: String? = null,
    val rating: Double,
    val badges: List<String>? = null,
    val settings: Settings
)

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

How do I use the JSON to Kotlin?

  1. Paste a JSON sample.
  2. Set the root type name, for example Order.
  3. Copy the Kotlin code into your project.

What does the JSON to Kotlin do?

  • @Serializable data classes
  • Nullable types with defaults for optional fields
  • List<T> for arrays
  • Nested objects become their own named types
  • 20 other languages from the same sample, including TypeScript

When to use the JSON to Kotlin

Android apps and Kotlin backends (Ktor, Spring) usually model API responses as data classes. Generating them from a real response means every field is there with the right type, ready for kotlinx.serialization, which works without reflection and is the usual choice for Kotlin Multiplatform too.

Each object becomes a @Serializable data class with val properties. Keys that aren't Kotlin-style names, like display-name or dark_mode, get @SerialName with a camelCase property. Whole numbers become Long and decimals Double, and a mix of both becomes Double. A field missing from some items becomes nullable with a default of null, so decoding doesn't fail when it's absent; a field whose values have different types becomes JsonElement for you to read by hand.

Frequently asked questions

Does it work with Moshi or Gson?
The data classes work with any library, but the annotations are kotlinx.serialization's. For Moshi, swap @SerialName for @Json(name = …); for Gson, @SerializedName.
Why does decoding fail on a field that isn't in my class?
kotlinx.serialization rejects unknown keys by default. Create the decoder with Json { ignoreUnknownKeys = true } so new fields in the API don't break your app.
How does it pick Kotlin types?
From the values in your sample: whole numbers, decimals, text, true/false, arrays and nested objects. When array items disagree, the field is made optional or widened.
Can I generate other languages?
Yes. Change Language to any of the others: TypeScript, Go, Python, Pydantic, Java, C#, Swift, Rust, Scala, Ruby, Dart, PHP, Zod, TypeBox, Valibot, Yup, GraphQL, Protobuf, Prisma and Drizzle.
Is my data uploaded?
No. Bracely runs entirely in your browser, so nothing you paste or open leaves your device.

More JSON tools

Bracely has 108 JSON tools in one page. Related ones:

Runs entirely in your browser. Nothing you paste is uploaded. Made by Saurabh Pagrut · About · Privacy policy
Last updated · Version 6.7 · Changelog
Saurabh Pagrut on GitHub · LinkedIn · X · YouTube · Instagram