JSON to Kotlin

  • Data class
  • kotlinx
  • Instant

Data classes with kotlinx.serialization annotations and nullable types.

Input
Kotlin

Paste input on the left to see kotlin.

Everything runs in your browser — nothing is uploaded.

About kotlin

Generating Kotlin types from JSON produces data classes annotated for kotlinx.serialization. JSONic adds SerialName wherever the camelCase property differs from the JSON key, and gives keys missing from some array elements a nullable type with a null default so decoding does not fail.

Paste JSON and get Kotlin data classes annotated for kotlinx.serialization. Property names become camelCase with a SerialName annotation wherever they differ from the JSON key, and keys absent from some array elements become nullable with a null default so deserialization does not fail on incomplete records.

Questions

Does this work with Moshi or Gson?

The data classes do. Replace @Serializable and @SerialName with @JsonClass and @Json for Moshi, or @SerializedName for Gson.

Why do nullable properties get a null default?

kotlinx.serialization throws on a missing field unless the property has a default. A null default makes absent keys decode cleanly instead of failing the whole payload.

Are Long or Int used for numbers?

Long for integral values, since JSON numbers have no declared width and a 64-bit id would silently overflow an Int.