JSON to Kotlin Converter
Generate Kotlin data classes from an API response, with null safety expressed properly in the type system.
Your data stays on your device
Input — JSON
- characters
- 0
- lines
- 0
- size
- 0 B
Output — Kotlin
- characters
- 0
- lines
- 0
- size
- 0 B
Options
About this tool
Kotlin distinguishes nullable and non-nullable types at compile time, which is precisely the distinction JSON leaves implicit. Getting it wrong produces either a null pointer exception at parse time or types littered with unnecessary question marks.
The generator marks a property nullable when the sample shows null or when the key is missing from some array elements, and leaves it non-null otherwise. Nested objects become their own data classes, and annotations are added for whichever serialiser you use.
How to use this tool
- Paste your JSON sample.
- Set the root class name.
- Choose the annotation style: none, kotlinx.serialization, or Gson.
- Press Generate and copy the data classes into your project.
Key features
- Idiomatic data class output with val properties
- Nullable types only where the sample justifies them
- kotlinx.serialization @Serializable and @SerialName, or Gson @SerializedName
- Default values of null for optional properties
- List<T> for arrays with unified element types
- Kotlin hard keywords escaped with backticks
Example
Input — JSON
{"item_id":5,"note":null}Output — Kotlin
@Serializable
data class Root(
@SerialName("item_id")
val itemId: Int,
@SerialName("note")
val note: String? = null
)Good to know
- kotlinx.serialization requires the compiler plugin in your build; the annotations alone are not enough.
- Nullability is inferred from a single sample, so a field that is occasionally null in production may be typed non-null here.
- No package declaration or imports are emitted.
Frequently asked questions
kotlinx.serialization or Gson?
kotlinx.serialization is the Kotlin-first choice: multiplatform, reflection-free and compile-time checked. Gson is still widespread on Android and needs no compiler plugin.
Why are some properties nullable?
Because the sample showed null, or the key was absent from at least one element of an array. Anything else is typed non-null so the compiler can help you.
Are default values generated?
Nullable properties default to null, which lets the serialiser construct the object when the key is missing. Non-null properties are required.
How are keys that are Kotlin keywords handled?
They are wrapped in backticks, and the original key is preserved in the serialisation annotation.
Your data stays on your device
Your data is processed locally in your browser and is not uploaded to our server. This page keeps working after you go offline, because there is nothing to send.