jsontoolskit.org
JSON and JSON Schema utilities, in the browser
Say hi →

JSON to Kotlin

JSON sample → data classes · updated 28 July 2026

Generate Kotlin data class declarations from a JSON sample. Choose kotlinx.serialization or Moshi; keys that differ from the Kotlin property name get @SerialName or @Json(name = …), and optional fields become nullable with an optional = null default.

Paste input to start.

How to generate Kotlin data classes from JSON

  1. Paste a JSON sample — ideally an array of several records, so optional fields are detected.
  2. Set the Root type name. Nested objects are named after the key that holds them, singularised for arrays.
  3. Pick your serialization library — kotlinx.serialization for multiplatform and modern Android, Moshi for existing Retrofit stacks.
  4. Leave = null defaults on so a missing key deserialises instead of throwing.
  5. Copy the code, or download it as a file.

Type mapping

Getting good types out of a sample

Type inference from a sample is only as good as the sample. Three habits make the output far more useful:

Privacy

Nothing is uploaded. Inference and code generation run in this tab. No server sees the payload you paste — which matters, because real API responses tend to contain real user data.

FAQ

Why does a default value matter?

With kotlinx.serialization, a non-nullable property with no default throws MissingFieldException when the key is absent. A = null default on a nullable property makes the field genuinely optional, which is what most APIs mean.

Do I need the serialization plugin?

For kotlinx.serialization, yes — add the plugin.serialization Gradle plugin and the kotlinx-serialization-json dependency. Moshi needs moshi-kotlin-codegen via KSP for @JsonClass(generateAdapter = true) to work.

Are the classes multiplatform-safe?

The generated code uses only stdlib types, so yes with kotlinx.serialization. Moshi is JVM-only.