Create a Kotlin data class from JSON automatically
This post describes how to use a plugin called “JSON To Kotlin Class” that automatically creates a Kotlin data class from JSON in Android Studio.
Imagine you have JSON that you need to send to or receive from a REST service. Depending on its size, manually creating a Kotlin data class can be tedious.
Wouldn’t it be nice to automatically generate a class from JSON? The “New” dialog in Android Studio only provides the usual options by default.

Wouldn’t it be great to have an option that takes JSON and creates the corresponding data class?
Luckily, plugins can help. To get started, go to “Settings → Plugins.”

To install “JSON To Kotlin Class,” search for “JSON,” select the plugin, and press “OK.”

After installing, you ’ll have a new menu entry to create a Kotlin data class from JSON.

Selecting it opens a window.

Provide a class name and paste your JSON into the field below.

Click “Generate” and then inspect the generated source file:
package com.example.apod
data class ApodDto(
val date: String,
val explanation: String,
val hdurl: String,
val media_type: String,
val service_version: String,
val title: String,
val url: String
)What do you think? This saves a lot of work, doesn’t it?
Thank you for reading!