JSON to Kotlin Data Class Generator — Free Online Tool
Convert JSON to Kotlin data class definitions with proper types, nullable fields, and kotlinx.serialization annotations using AI. Free, no sign-up.
JSON to Kotlin Data Class Generator — Free Online Tool
About JSON to Kotlin Data Class Generator — Free Online Tool
This tool converts JSON objects into Kotlin data classes with kotlinx.serialization annotations, automatically inferring types for all fields including nested objects, arrays, and nullable values. Developers use it to eliminate the tedious manual work of writing boilerplate data classes when integrating REST APIs or parsing JSON responses in Android and Kotlin Multiplatform projects.
JSON to Kotlin Type Mapping
| JSON Type | Example Value | Kotlin Type | Nullable Variant |
|---|---|---|---|
| string | "hello" | String | String? |
| number (integer) | 42 | Int | Int? |
| number (large integer) | 9876543210 | Long | Long? |
| number (decimal) | 3.14 | Double | Double? |
| boolean | true | Boolean | Boolean? |
| null | null | Any? | — |
| object | {"key": ...} | Nested data class | NestedClass? |
| array of strings | ["a", "b"] | List<String> | List<String>? |
| array of objects | [{...}, {...}] | List<NestedClass> | List<NestedClass>? |
| empty array | [] | List<Any> | List<Any>? |
kotlinx.serialization vs Gson vs Moshi — Feature Comparison
| Feature | kotlinx.serialization | Gson | Moshi |
|---|---|---|---|
| Kotlin Multiplatform support | Yes | No | No |
| Null safety enforcement | Yes | No | Yes |
| Code generation approach | Compiler plugin | Reflection | Codegen (kapt/ksp) |
| @SerialName annotation | Yes | No (@SerializedName) | Yes (@Json) |
| Default values respected | Yes | Partial | Yes |
| Sealed class support | Yes (polymorphism) | Limited | Limited |
| Kotlin coroutines integration | Native | Manual | Manual |
| Android recommended by Google | Yes (2023+) | Legacy | Supported |
| Gradle plugin required | Yes | No | No (KSP optional) |
Frequently Asked Questions
How do I add kotlinx.serialization to my Android project?
Add the Kotlin serialization plugin to your build.gradle: `id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0'` in the plugins block, then add the runtime dependency `implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0'`. The compiler plugin is required — without it, the @Serializable annotation will compile but throw a runtime exception because no serializer is generated.
What does @SerialName do in kotlinx.serialization?
@SerialName maps a Kotlin property name to a different JSON key, which is essential when the API uses snake_case (e.g., user_id) but you want idiomatic Kotlin camelCase (e.g., userId). Without @SerialName, the property name in your data class must match the JSON key exactly or deserialization returns null/default values. Example: `@SerialName("user_id") val userId: String`.
Why are some fields generated as nullable (String?) in my Kotlin data class?
Fields are generated as nullable when the JSON sample contains a null value for that key, or when the key is missing in some objects within a JSON array. Kotlin enforces null safety at compile time, so the generator marks any field that could legitimately be absent or null as nullable to prevent NullPointerExceptions at runtime. If you know a field will always be present, you can remove the `?` manually.
Can kotlinx.serialization handle nested JSON objects and arrays?
Yes — nested JSON objects become separate @Serializable data classes that are referenced as property types, and JSON arrays map to List<T> where T is the inferred element type. For deeply nested structures, the generator creates multiple data classes in the same file. kotlinx.serialization handles arbitrary nesting depth as long as all involved classes are annotated with @Serializable.
How do I handle JSON fields with names that are Kotlin reserved keywords?
Use backtick escaping combined with @SerialName: annotate the property with `@SerialName("object")` and name the Kotlin property something idiomatic like `objectData`, or use backticks like `val \`object\`: String` if you must keep the original name. The @SerialName approach is cleaner and more readable. Reserved keywords that commonly appear in JSON include object, class, in, is, val, and return.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.