About JSON to Kotlin Online
Paste a JSON object and instantly get Kotlin data class definitions. Handles nested objects, arrays (as List<T>), and all JSON types mapped to their Kotlin equivalents. Generated code is ready for use with Gson, Moshi, or kotlinx.serialization.
Frequently Asked Questions
What is JSON to Kotlin data class generation?
JSON to Kotlin data class generation analyses a JSON sample and produces Kotlin data class definitions with @Serializable and @SerialName annotations, compatible with the kotlinx.serialization library and ready for use with Json.decodeFromString().
Which serialization library should I use: kotlinx.serialization, Gson, or Moshi?
kotlinx.serialization is the official Kotlin choice and the best for multiplatform projects. Gson is simple but does not support Kotlin nullable types correctly without adapters. Moshi with kotlin-reflect or codegen is a solid alternative with full null-safety support. For Android projects, kotlinx.serialization or Moshi are preferred; for Spring Boot, Jackson with kotlin-module is standard.
How are nullable JSON fields represented in a Kotlin data class?
Fields that are null in the JSON sample are typed as nullable (String?, Int?, etc.) with a default value of null. This lets Kotlin handle missing fields gracefully without a NullPointerException. Enable the "Make all optional" toggle to mark every field as nullable — recommended when your real API may return more nulls than the sample shows.
Can I use the generated data class directly with Retrofit?
Yes. Add the kotlinx.serialization or Moshi converter factory to your Retrofit.Builder and annotate your data class with @Serializable (kotlinx) or leave it plain (Moshi with codegen). Retrofit will automatically deserialise the JSON response body into your data class. No manual parsing code is needed.
How do I map camelCase JSON keys to Kotlin snake_case properties?
Add @SerialName("snake_case_key") to each property, or configure a NamingStrategy at the library level. With kotlinx.serialization: use @SerialName per field. With Moshi: use @Json(name = "snake_case_key"). With Gson: use @SerializedName("snake_case_key"). The generator outputs camelCase properties matching standard Kotlin conventions.
Advertisement300 × 250
Advertisement300 × 250