About this tool
Paste a JSON response and get Kotlin data classes ready for Android or server-side Kotlin. Properties use camelCase names, and an annotation maps them back to the original key whenever it differs (full_name → fullName). Arrays become List<T>, and values that were null or missing in the sample become nullable types.
Libraries
- kotlinx.serialization: the official Kotlin library, multiplatform and reflection-free. Classes get
@Serializableand renamed properties@SerialName. Decode withJson.decodeFromString<Root>(json). - Moshi:
@JsonClass(generateAdapter = true)with codegen, and@Json(name = …)for renamed properties. Popular with Retrofit. - Gson:
@SerializedName. Note that Gson ignores Kotlin defaults and non-null types, so prefer one of the others for new code.
Nullability and defaults
With Default nullable fields to null on, optional properties get = null, so JSON without them still decodes. kotlinx.serialization also needs Json { ignoreUnknownKeys = true } if the API may add fields you didn’t model. Whole numbers become Int, or Long above 32 bits; decimals become Double.
How types are inferred
- Every object becomes its own named type. Names come from the key (
address→Address), and items of an array get the singular (orders→Order). - For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes
nullbecomes nullable. - Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
- Identical nested shapes are reused instead of generating duplicates.
The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.
FAQ
How do I convert JSON to a Kotlin data class?
Paste the JSON above. A data class is generated for every object, with annotations that map camelCase properties to the original keys.
kotlinx.serialization, Moshi or Gson?
kotlinx.serialization for new projects and Kotlin Multiplatform; Moshi if you already use it with Retrofit. Gson works but doesn’t understand Kotlin null safety or default values.
Why are some properties nullable?
They were null or missing in the sample JSON, so the API may leave them out. Check the API documentation and remove the ? where a value is guaranteed.
What happens with unknown fields?
kotlinx.serialization fails on unknown keys by default; configure Json { ignoreUnknownKeys = true }. Moshi and Gson ignore them.