FormatterHQ

JSON to Kotlin Data Class Generator

Generate Kotlin data classes from JSON: immutable val properties, nullable types with default values, nested classes, and optional Jackson parsing code.

Input JSON
Output KOTLIN
Ready.

How to use the JSON to Kotlin

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. Tick Types only (no helpers) for plain data classes, or leave it off to include Jackson annotations with fromJson / toJson.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to Kotlin: data classes from a sample

Kotlin's data class is the natural shape for JSON models: immutable properties, generated equals, hashCode, toString and copy. This JSON to Kotlin generator writes data classes from a sample, one per object shape, using val properties, Kotlin types, and nullable types where the data shows a value can be missing or null.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}

With Types only checked:

package quicktype

import java.time.OffsetDateTime

data class Task (
    val id: Long,
    val title: String,
    val done: Boolean,
    val priority: Double,
    val tags: List<String>,
    val owner: Owner,
    val due: OffsetDateTime,
    val subtasks: List<Subtask>
)

data class Owner (
    val name: String,
    val email: Any? = null
)

data class Subtask (
    val title: String,
    val estimate: Long? = null
)

Optional properties are nullable and default to null, so constructing a Subtask without an estimate compiles. The date string became java.time.OffsetDateTime, which is available on the JVM and on Android API 26+.

Jackson helpers in full mode

With Types only off, the output targets Jackson with the Kotlin module: properties get @get:JsonProperty(required=true) and @field:JsonProperty(required=true) annotations, a preconfigured mapper handles OffsetDateTime and skips nulls when writing, and each top-level class gets toJson() and Task.fromJson(jsonString). Add com.fasterxml.jackson.module:jackson-module-kotlin to your dependencies, as the header comment explains.

Using kotlinx.serialization, Moshi or Gson instead

The types-only classes are a good base for other libraries:

  • kotlinx.serialization: annotate each class with @Serializable, add @SerialName("user_name") where keys differ, and use a String or a custom serializer for dates, since OffsetDateTime has no built-in serializer.
  • Moshi: add @JsonClass(generateAdapter = true) and @Json(name = "..."), the usual choice in Android apps using Retrofit.
  • Retrofit: declare suspend fun getTask(@Path("id") id: Long): Task in your service interface and register a converter factory for the library you chose; the data class is used as the return type directly.
  • Gson: works with data classes, but it ignores Kotlin's null-safety and default values, so a missing non-null field can end up null at runtime.

Whatever library you use, property names are converted to camelCase (is_active becomes isActive), so add name annotations when the JSON uses snake_case.

Null safety and missing keys

Kotlin's type system makes a promise: a String is never null. JSON libraries must keep that promise when data is incomplete. With Jackson's Kotlin module, a missing value for a non-null property without a default causes a MismatchedInputException (reported for Kotlin as a missing parameter). kotlinx.serialization throws MissingFieldException. Both are good: the error appears at the boundary instead of as a NullPointerException later. If an API field is sometimes absent, make the property nullable with a default, like the generated estimate: Long? = null.

Type mapping

JSONKotlin
integer / decimalLong / Double
booleanBoolean
ISO date-timeOffsetDateTime
arrayList<T>
optional / nullableT? = null
null onlyAny?

Replace the quicktype package with your own. For XML sources, use XML to Kotlin; for Java models, JSON to Java. Check the input with the JSON Validator. Generation runs in your browser.

Frequently asked questions

Can I use the generated data classes with kotlinx.serialization?
Yes, with Types only on. Add @Serializable to each class, @SerialName where JSON keys differ, and handle dates with a string or a custom serializer.
Why do optional properties default to null?
Properties missing from some objects in the sample are nullable, and a default of null lets you create instances without them and lets libraries that honor defaults handle absent keys.
Does it work for Android?
Yes. Data classes work with Moshi, Gson and kotlinx.serialization. OffsetDateTime needs API 26+ or core library desugaring; change dates to String otherwise.
Which JSON library does full mode use?
Jackson with jackson-module-kotlin. The output includes annotations, a configured ObjectMapper and fromJson/toJson functions.

Last updated September 26, 2026

Related tools

XML to Kotlin Generate Kotlin data classes from a XML sample instantly, with nested types detected automatically. JSON to Laravel Model & Migration Generate a Laravel Eloquent model ($fillable, casts, relations) and a matching migration from a sample JSON record. JSON to Mongoose Schema Generate a Mongoose schema and model from a sample JSON document, detecting Date, ObjectId, arrays and nested objects. JSON to Objective-C Generate Objective-C classes with properties from a JSON sample instantly, with nested types detected automatically. JSON to PHP Generate PHP classes with typed properties from a JSON sample instantly, with nested types detected automatically. JSON to Pike Generate Pike classes from a JSON sample instantly, with nested types detected automatically. JSON to Python Generate Python dataclasses with type hints from a JSON sample instantly, with nested types detected automatically. JSON to Ruby Generate Ruby Dry::Struct classes from a JSON sample instantly, with nested types detected automatically. JSON to Rust Generate Rust structs with Serde derives from a JSON sample instantly, with nested types detected automatically. JSON to Scala Generate Scala case classes from a JSON sample instantly, with nested types detected automatically. JSON to Swift Generate Swift Codable structs from a JSON sample instantly, with nested types detected automatically. JSON to TypeScript Generate TypeScript interfaces from a JSON sample instantly, with nested types detected automatically.