Skip to content
jsonforge.app

JSON to Kotlin

Generate Kotlin data classes from a JSON sample for kotlinx.serialization, Moshi, or Gson.

JSON input

Loading editor…

Generated code

Enter JSON on the left to generate a type from it.

What is JSON to Kotlin?

A JSON to Kotlin converter turns a sample JSON payload into Kotlin data classes matching its structure. The data class is Kotlin's canonical way to model a JSON response: declaring the properties in the constructor is enough for the compiler to generate equals, hashCode, toString, copy, and destructuring, so a model that would be a long POJO in Java becomes a handful of lines. This is the shape every Kotlin JSON library expects — kotlinx.serialization, Moshi, and Gson all bind into data classes — which makes generating them from a real payload the fastest route from an API response to typed, null-safe Kotlin. It also removes the frequent Android integration bug where a property name differs from the JSON key by one character and quietly deserializes as null.

How to use JSON to Kotlin

  1. Paste a representative JSON response into the left panel — the generated properties match exactly the keys in that sample.
  2. Set a root class name above the output (defaults to "Root"); each nested object becomes its own data class named after the key holding it.
  3. Copy the classes into your project and add whatever your JSON library needs — @Serializable for kotlinx.serialization, or nothing at all for Moshi and Gson with reflection.
  4. Switch the language picker above the output to emit the same shape as a Java record, Go struct, or TypeScript interface in another supported language.

Examples

Flat object as a data class

Input

{"id": 1, "name": "Ada", "active": true}

Output

data class Root( val id: Int, val name: String, val active: Boolean, )

Nested object and a list

Input

{"user": {"id": 1, "name": "Ada", "roles": ["admin"]}}

Output

data class User( val id: Int, val name: String, val roles: List<String>, ) data class Root( val user: User, )

Common mistakes

  • Leaving properties non-nullable when the API omits them — kotlinx.serialization throws a MissingFieldException, and Gson can silently place a null into a non-null property, breaking Kotlin's null-safety guarantee in a way that crashes far from the cause.
  • Adding @Serializable without also applying the kotlinx.serialization Gradle plugin, which fails at compile time with a message that doesn't obviously name the missing plugin.
  • Keeping Int for millisecond timestamps or large IDs, which overflows silently rather than raising an error.

Why use this tool

  • Produces idiomatic data classes, so equals, hashCode, toString, and copy come free rather than being written out.
  • Gives every nested object its own named class, which is what Retrofit, Moshi, and kotlinx.serialization all expect.
  • Runs entirely client-side, so a payload copied from an Android or Ktor service never leaves your machine to generate these data classes.

Frequently asked questions