Developer Tools

JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for Android or server-side Kotlin. Choose kotlinx.serialization, Gson or Moshi annotations; properties use camelCase with the original key preserved where it differs, nullable types with = null defaults mark optional fields, and integer sizes follow the values in the sample.

  • Runs in your browser
  • No sign-up
  • Free to use

How to use JSON to Kotlin Data Class

  1. Paste a JSON sample.
  2. Choose the serialization library.
  3. Pick val or var and default values.
  4. Copy the classes or download Models.kt.

JSON to Kotlin Data Class features

Three libraries

kotlinx.serialization, Gson and Moshi annotations.

Null safety

Type? with = null defaults for optional or null fields.

Key mapping

@SerialName, @SerializedName or @Json only where names differ.

Number sizes

Int or Long by value range, Double for decimals.

Immutable by default

val properties, with a var option.

Local

Generated in your browser.

When to use JSON to Kotlin Data Class

  • Modelling a Retrofit API response in an Android app.
  • Creating DTOs for a Ktor server.
  • Parsing JSON configuration in a Kotlin Multiplatform project.
  • Replacing hand-written Gson models with kotlinx.serialization.

JSON to Kotlin Data Class FAQ

Which library should I use?

kotlinx.serialization is Kotlin-native and works in Multiplatform projects. Moshi is popular on Android and respects null safety. Gson is older and widely used but bypasses Kotlin’s null checks.

Why do some properties have = null?

Fields that are missing or null in the sample are optional. A default lets the JSON omit them; without it, kotlinx.serialization throws on missing fields.

What type is used for unknown values?

JsonElement with kotlinx.serialization, or Any with Gson and Moshi.

Are dates converted?

No. Kotlin libraries need custom serializers for dates, so date strings stay String. Convert them after parsing, or add a serializer.

Why data classes?

Data classes provide equals, hashCode, toString and copy automatically, which suits value objects decoded from JSON.

Is anything uploaded?

No. Everything happens in your browser.

Kotlin data classes for JSON

Kotlin developers usually model JSON with data classes: a primary constructor listing typed properties, with equality, hashing, copying and a readable toString generated by the compiler. A serialization library then maps JSON keys to those properties. Getting the property types and nullability right is what makes the difference between models that work and models that crash on the first unusual response.

Nullability is part of Kotlin’s type system, so the generator takes it seriously. When the sample shows a field missing from some objects or set to null, the property becomes nullable with a default of null, which lets the library accept JSON without the field. Fields that were always present stay non-null. Review these choices, because a sample can make an optional field look required.

The annotations depend on the library. kotlinx.serialization needs @Serializable on each class and @SerialName where the property name differs from the key. Gson uses @SerializedName and Moshi uses @Json, with @JsonClass(generateAdapter = true) for code-generated adapters. Annotations are added only where needed, so the classes stay readable.

Numbers follow the sample: integers that fit in 32 bits become Int, larger ones Long, and decimals Double. Values with mixed types become JsonElement for kotlinx.serialization, which can be inspected safely, or Any for the other libraries. Arrays become List of the merged element type.

One caveat concerns Gson. It creates objects without calling Kotlin constructors, so non-null properties can end up null and default values are ignored. If you rely on null safety, prefer kotlinx.serialization or Moshi, both of which are supported here.

Other useful tools