← All tools Code Gen

JSON to Kotlin Data Classes

Paste any JSON and generate production-ready Kotlin data classes with proper types, nullable fields, and optional @SerializedName annotations. Everything runs in your browser.

Why Convert JSON to Kotlin Data Classes?

Every Android and backend Kotlin project consumes JSON from REST APIs, configuration files, or third-party services. Manually writing data classes for each API response is tedious and error-prone. A single mistyped property name or wrong type can cause runtime crashes that are hard to debug.

Automated conversion eliminates that boilerplate. Paste the raw JSON response, click generate, and get type-safe data classes ready to drop into your project. The tool infers the correct Kotlin types, handles nested objects as separate classes, and marks nullable fields so the compiler catches potential null-pointer issues at build time.

Kotlin Type Mapping from JSON

Primitives
  • • JSON string → String
  • • JSON integer → Int
  • • JSON decimal → Double
  • • JSON boolean → Boolean
  • • JSON null → Any?
[] Complex Types
  • • Nested object → separate data class
  • • Array of objects → List<ClassName>
  • • Array of strings → List<String>
  • • Array of numbers → List<Int>
  • • Empty array → List<Any>

@SerializedName and Property Naming

JSON APIs commonly use snake_case for field names, but Kotlin convention is camelCase. When you enable the @SerializedName option, the tool generates camelCase property names and adds the Gson annotation pointing to the original JSON key. This gives you idiomatic Kotlin code that correctly maps to the raw JSON.

JSON: "user_name": "Alice"
Kotlin: @SerializedName("user_name") val userName: String

Best Practices for Kotlin Data Classes

  1. 1

    Use val, not var. Immutable properties make your data classes thread-safe and predictable. Use the copy() function when you need a modified version of an instance.

  2. 2

    Mark nullable fields explicitly. Kotlin null safety catches missing fields at compile time. If an API field might be absent, use the nullable type (e.g. String?) so the compiler enforces safe access.

  3. 3

    Keep data classes in their own file. One file per top-level data class (or a file grouping related models) keeps your project navigable, especially when models multiply as your API grows.

  4. 4

    Add default values for optional fields. Providing defaults means the class can be constructed even when some JSON fields are missing, preventing deserialization failures.

  5. 5

    Consider kotlinx.serialization for multiplatform. If you target Kotlin Multiplatform, kotlinx.serialization works across JVM, JS, and Native. Replace @SerializedName with @SerialName and add the @Serializable annotation.

Frequently Asked Questions

What is a Kotlin data class?

A Kotlin data class is a class whose primary purpose is to hold data. The compiler automatically generates equals(), hashCode(), toString(), copy(), and componentN() functions from the properties declared in the primary constructor. They are the idiomatic way to model JSON responses and DTOs in Kotlin.

What does @SerializedName do in Kotlin?

@SerializedName is a Gson annotation that maps a JSON field name to a Kotlin property name. For example, if the JSON key is "user_name" but your Kotlin property is "userName", the annotation tells Gson how to correctly serialize and deserialize the field.

How are nested JSON objects handled?

Nested JSON objects are converted into separate Kotlin data classes. Each nested object becomes its own class with a name derived from the JSON key in PascalCase. The parent class references it by type, creating a clean, composable class hierarchy.

How does the tool handle JSON arrays?

JSON arrays are mapped to Kotlin List types. Arrays of objects generate a new data class for the item type. Primitive arrays become List of String, Int, etc. Empty arrays default to List of Any since the element type cannot be inferred without data.

Can I use this with Moshi or kotlinx.serialization instead of Gson?

Yes. The generated data classes work with any serialization library. The @SerializedName annotation is Gson-specific, but you can disable it and manually add @Json for Moshi or @SerialName for kotlinx.serialization. The val properties and nullable types remain the same across all libraries.

Related Tools