Skip to main content
CheckTown
Converters

How to Convert JSON to Kotlin Data Classes

Published 5 min read
In this article

Why Kotlin Data Classes from JSON

Kotlin data classes provide a concise way to model JSON data with built-in equals, hashCode, copy, and destructuring support. For Android developers and backend Kotlin projects, manually writing data classes for every API response is time-consuming and error-prone. Our JSON to Kotlin converter generates idiomatic data classes with proper null safety, serialization annotations, and naming conventions in seconds.

Kotlin's null safety system is one of its strongest features, but it requires explicit nullable type declarations. When converting JSON — where any field can potentially be null — the converter intelligently analyzes the data to determine which fields should be nullable (marked with ?) and which can safely be non-null, producing safer code from the start.

Converter Features

The converter produces Kotlin data classes tailored to your serialization library and project requirements.

  • Nullable types — fields with null values in the JSON sample generate nullable types (String?), while present values generate non-null types (String), accurately reflecting the data contract
  • @SerializedName — generates Gson @SerializedName annotations when JSON keys use snake_case or other non-camelCase formats, mapping them to idiomatic Kotlin property names
  • Default values — optional fields can include default values (empty strings, empty lists, null) in the constructor, making the data class flexible for partial JSON responses

Try it free — no signup required

Convert JSON to Kotlin →

Android-Specific Patterns

Kotlin data classes generated from JSON are particularly useful in Android development workflows.

  • Parcelize — generated data classes can be annotated with @Parcelize and implement Parcelable for passing between Android activities and fragments without writing any parcel code
  • Room integration — the output can serve as Room entity classes by adding @Entity and @PrimaryKey annotations, bridging API responses directly to local database storage
  • Retrofit compatibility — the generated classes work immediately with Retrofit and Gson/Moshi converters, letting you define API interfaces and start making network calls within minutes

Frequently Asked Questions

How does the converter handle null safety?

The converter examines each field in the JSON sample. Fields with null values are typed as nullable (e.g., String?). Fields with actual values are typed as non-null (e.g., String). For the most accurate null safety, provide a JSON sample that represents the full range of your data — including fields that can be null in production.

Should I use val or var for data class properties?

The converter generates val (read-only) properties by default, following Kotlin's preference for immutability. Data classes with val properties are thread-safe and easier to reason about. If you need mutable properties for specific use cases like Room entities with auto-generated IDs, you can change individual properties to var after generation.

Can I generate sealed classes for polymorphic JSON?

The converter generates standard data classes from the JSON structure. For polymorphic JSON (where a field determines the object type), you would need to manually create a sealed class hierarchy after generation. Use the converter to generate each variant as a separate data class, then wrap them in a sealed class with a discriminator field.

Related Tools