01What is JSON to Kotlin?
Hand-writing Kotlin models for a JSON API is slow and easy to get wrong. One misspelled property or a missed nullable field, and your Retrofit call fails at runtime. This tool reads a JSON sample and writes Kotlin data classes for it, with one val property per key. The type rules follow the Kotlin language docs for basic types. Strings become String and booleans become Boolean. Whole numbers become Int, and switch to Long when a value does not fit in 32 bits. Any number with a decimal part becomes Double. A null value becomes Any?, because one null tells us nothing about the real type. Nested objects get their own data class, named in PascalCase after the key. Keys in snake_case or kebab-case turn into camelCase properties, so user_id becomes userId. Kotlin hard keywords such as class or object are kept and wrapped in backticks. Other invalid characters become underscores. Arrays are handled by merging every element into one sample. An array of objects yields a single class holding every key seen in any element. A key that is missing or null in some elements becomes nullable. Number lists with any decimal become List<Double>, and lists mixing JSON types become List<Any>. Here is a quick example. Paste {"items":[{"id":1},{"id":2,"qty":3}]} and you get a Root class with val items: List<Items>. You also get data class Items(val id: Int, val qty: Int?). The qty field is nullable because the first item lacks it. A few edge cases are covered too. An empty object becomes a plain class with no properties, since a Kotlin data class needs at least one constructor parameter. A class that would be named List, String or Map gets a numeric suffix like List2, so it never hides the built-in type. A top-level array of primitives produces a typealias, such as typealias Tags = List<String>. Now the honest limits. The output has no serialization annotations at all. Renamed properties will not match their JSON keys until you add @SerialName for kotlinx.serialization, @Json for Moshi or @SerializedName for Gson. Nullable properties get no default value either. Types come only from your sample, so a field that happens to be a whole number there will be typed Int. The parsing uses the browser's own JSON.parse, so your payload stays on your machine.
Android and Kotlin backend developers who need typed models for a JSON API without writing every data class by hand. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Kotlin
- Paste your JSON into the JSON Input box, or pick one of the examples.
- Type the root class name in the Class name field. It defaults to Root.
- Read the generated classes in Kotlin Output. If the JSON is invalid, a JSON Parse Error message appears instead.
- Click Copy to put the code on your clipboard.
- Click Reset to clear the input and set the class name back to Root.
03Worked examples
- Input:
{"user_id":7,"full-name":"Ann","price":9.99,"address":{"zip_code":"0150"},"meta":null} - Result:
data class Root( val userId: Int, val fullName: String, val price: Double, val address: Address, val meta: Any? ) data class Address( val zipCode: String ) - Keys become camelCase, the nested object gets its own class, and null becomes Any?.
- Input:
{"class":1,"items":[{"id":1},{"id":2,"qty":3}],"empty":[]} - Result:
data class Root( val `class`: Int, val items: List<Items>, val empty: List<Any> ) data class Items( val id: Int, val qty: Int? ) - The keyword class is wrapped in backticks. qty is missing from the first item, so it becomes Int?.
- Input:
{"big":9007199254740993,"mixed":[1,"a"],"ints":[1,2.5]} - Result:
data class Root( val big: Long, val mixed: List<Any>, val ints: List<Double> ) - A value beyond Int range becomes Long. Mixed arrays become List<Any>, and one decimal makes the whole list Double.
- Input:
[{"id":1,"name":"x"},{"id":2,"name":null}] with Class name User - Result:
data class User( val id: Int, val name: String? ) - A top-level array of objects builds the root class from all elements. name is null in one item, so it becomes String?.
- Input:
["a","b"] with Class name Tags - Result:
typealias Tags = List<String> - A top-level array of primitives has no class to build, so you get a typealias.
- Input:
{"list":{"a":1},"settings":{}} - Result:
data class Root( val list: List2, val settings: Settings ) data class List2( val a: Int ) class Settings - List would shadow Kotlin's List, so the class becomes List2. The empty object becomes a plain class.
04Common use cases
- Writing Retrofit response models for an Android screen that calls a new REST endpoint
- Creating DTOs for a Ktor or Spring Boot service that consumes a partner's JSON API
- Modelling a JSON config file that a Kotlin CLI tool loads at startup
- Sketching shared data classes for a Kotlin Multiplatform module used by Android and iOS
- Checking which fields in a paginated API response are sometimes missing, by pasting several items and seeing which turn nullable
- Turning a webhook payload from a log into typed classes for a unit test fixture
05Tips
- Paste an array with several real elements, not just one. Keys that only some elements carry still show up, typed as nullable.
- Add @SerialName (kotlinx.serialization) or @SerializedName (Gson) to any property whose name changed from snake_case, or the field will stay empty.
- Swap Any? for a concrete type like String? once you know what that field holds. Any? pushes casts into every caller.
- Give nullable properties a default of = null. Otherwise kotlinx.serialization throws when the key is missing from the response.
- Watch for classes named List2 or String2. The suffix means a key clashed with a Kotlin built-in name, so rename the class to something meaningful.
06Related concepts
Kotlin data classes, kotlinx.serialization, Retrofit, Moshi, Type inference.
Frequently Asked Questions
What is JSON to Kotlin?
JSON to Kotlin is a free, browser-based developer tool on AIDToolStack. Convert JSON to Kotlin data classes It requires no installation or sign-up and works entirely in your browser.
Is JSON to Kotlin free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Kotlin store my data?
No. It runs in your browser; nothing is uploaded.