{}JSONLint.app

JSON to Kotlin Converter

Generate Kotlin data classes from JSON. Paste your JSON:

Loading...
Loading...

Generate Kotlin Data Classes

This tool converts JSON to Kotlin data classes with support for popular serialization libraries: kotlinx.serialization, Moshi, and Gson.

Example Output

@Serializable
data class User(
    val id: Int,
    @SerialName("user_name") val userName: String,
    val email: String,
    @SerialName("is_active") val isActive: Boolean
)

Serialization Options

LibraryAnnotationBest For
kotlinx.serialization@SerializableKotlin Multiplatform, modern projects
Moshi@JsonAndroid, Square ecosystem
Gson@SerializedNameLegacy Android projects

Type Mapping

JSONKotlin
stringString
integerInt / Long
decimalDouble
booleanBoolean
nullString?
arrayList<T>
objectNested data class

Related Tools

Common Mistakes & Pro Tips

  • Nullability is enforced — use ? for optional/null fieldsKotlin's type system distinguishes `String` from `String?`. A field that can be null or missing in JSON must be declared nullable (`val email: String?`), or deserialization can throw or produce a non-null contract violation. kotlinx.serialization will fail on a missing non-nullable field unless you also give it a default.
  • Map JSON keys with the right annotation for your libraryKotlin properties are camelCase, so snake_case keys need mapping. With kotlinx.serialization use `@SerialName("created_at")`; with Moshi use `@Json(name = "created_at")`; with Gson use `@SerializedName("created_at")`; with Jackson use `@JsonProperty("created_at")`. These are not interchangeable, so generate for your actual serializer.
  • Defaults make a property optional in kotlinx.serializationkotlinx.serialization treats a property as optional only if it has a default value (`val page: Int = 1`) — otherwise a missing key throws `MissingFieldException`. Combine a default with nullability (`val note: String? = null`) to tolerate both missing and null. Gson/Moshi are more lenient and leave missing fields null even without a default.
  • Numbers map to Int/Long/Double; pick width carefullyWhole numbers default to `Int` (32-bit); use `Long` for large IDs and millisecond timestamps that exceed ~2.1 billion. Decimals map to `Double`, which is imprecise for money — use `BigDecimal` there. Kotlin won't auto-widen, so an out-of-range value can overflow or fail to parse into the declared type.
  • Empty arrays, dates, and hard keysAn empty array `[]` has no element type and falls back to `List<Any?>` (or a placeholder) — narrow it manually. Date strings remain `String` unless you add a custom serializer for `LocalDateTime`/`Instant`. JSON keys that are Kotlin hard keywords or contain invalid characters need backticks (`` `is` ``) or a renamed property plus a `@SerialName`/`@Json` mapping.

Frequently Asked Questions

Why are these data classes?

`data class` auto-generates `equals`, `hashCode`, `toString`, `copy`, and `componentN` functions, which is exactly what you want for immutable JSON DTOs. Use `val` properties for immutability and `copy()` to create modified instances. A regular class would force you to write all that boilerplate yourself.

How do I handle optional or nullable fields?

Declare the property nullable with `?` and, for kotlinx.serialization, give it a default so a missing key doesn't throw: `val bio: String? = null`. Gson and Moshi will leave an absent field as null without a default, but Moshi with Kotlin codegen still enforces non-null for non-optional properties, so prefer `?` + default for safety. Defaults are what make a key truly skippable in kotlinx.serialization.

Which serialization library should I target — kotlinx, Moshi, or Gson?

kotlinx.serialization is the official multiplatform choice and integrates tightly with Kotlin nullability and defaults via `@Serializable`/`@SerialName`. Moshi (with the Kotlin codegen) is popular on Android and respects nullability without reflection. Gson is widely used but reflection-based and unaware of Kotlin's non-null types, so it can put null into a non-null property. Generate for the one your project uses.

What does @Serializable do and do I need it?

`@Serializable` marks a class for kotlinx.serialization's compiler plugin, which generates the serializer so `Json.decodeFromString` / `Json.encodeToString` work without reflection. You only need it if you use kotlinx.serialization; Moshi/Gson/Jackson don't require it. Every nested type used by a `@Serializable` class must also be `@Serializable`.

Why is a number field typed as Int instead of Long?

The generator infers `Int` from a small whole-number sample, but Int is 32-bit and overflows around 2.1 billion. For database IDs, Unix-millisecond timestamps, or large counters, change the type to `Long` manually. For exact decimals like currency, use `BigDecimal` rather than `Double`.

Does my JSON get sent anywhere?

No. The data classes are generated entirely in your browser with no server upload, so your JSON stays local. That makes it safe for internal Android/Kotlin API models or payloads containing secrets.