Open In App

Integrating JsonToKotlin Plugin With Android Studio

Android Studio is the best IDE for android development. Plenty of plugins are available and they can be installed easily via Android Studio itself. In computing, a plug-in is a software component that adds a particular characteristic to an existing computer program. When a program supports plug-ins, it enables customization. Plugins are a great way to increase productivity and overall programming experience.⁣⁣ Some tasks are boring and not fun to do, by using plugins in the android studio you can get more done in less time. One such plugin that we are going to see in this article is “JsonToKotlin Plugin“.

Note: You may refer to the 6 Most Useful Android Studio Plugins to know the most useful android studio plugins.



About JsonToKotlin Plugin

JSON to kotlin Class is a plugin to create Kotlin data class from JSON string, in other words, a plugin that changes JSON string to Kotlin data class. With this, you can generate a Kotlin data class from the JSON string programmatically. Supporting (almost) all kinds of JSON libs’ annotation(Gson, Jackson, Fastjson, MoShi and LoganSquare, kotlinx.serialization(default custom value)). Some of the important features are:

JSON to kotlin Class is an excellent tool for Kotlin developers and it can convert a JSON string to Kotlin data class. The tool could not only understand the primitive types but also auto-create complex types. It’s simply accessible. We provide shortcut keymap ALT + K for Windows and Option + K for Mac, have a try and you are going to fall in love with it! JsonToKotlinClass just makes programming more delightful.



Installation of JsonToKotlin Plugin

Note: You may refer to How to Install and Uninstall Plugins in Android Studio? to Install and Uninstall Plugins in Android Studio.

Step 1: Go to the File > Settings > Plugins > Search for jsontokotlin as shown in the below image.

Step 2: Press the install button

Step 3: Once you got the plugin, please install it by clicking on the “Install” button. Then select the project location and right-click on that folder, select New > Kotlin data class file from JSON (as shown in the image below)

Step 4: Then in the place of JSON provision, provide the JSON that you want to convert. In our example, let us give  the sample output of JSON that is meant for Exchange Rates as shown in the below image

Once it is given and generate, we can able to see 2 files got generated as shown in the below-attached image. Here two data class are generated.

ExchangeRates” is the data class nothing but the name that we specified  If we check the JSON output and generated data class “ExchangeRates”, all the column names like

is created very easily.




// JSON to kotlin conversion is 
// easier with JSONToKotlin plugin
data class ExchangeRates(
    val base: String,
    val date: String,
    val rates: Rates,
    val time_last_updated: Int
)

And since many “Rates” are there, which are nothing but the exchange rate value of all the available worldwide currencies, it has got created with another data class.




// In JSON, below are the currencies 
// available and they are presented here
data class Rates(
    val AED: Double,
    val ARS: Double,
    val AUD: Double,
    val BGN: Double,
    val BRL: Double,
    val BSD: Int,
    val CAD: Double,
    val CHF: Double,
    val CLP: Double,
    val CNY: Double,
    val COP: Double,
    val CZK: Double,
    val DKK: Double,
    val DOP: Double,
    val EGP: Double,
    val EUR: Double,
    val FJD: Double,
    val GBP: Double,
    val GTQ: Double,
    val HKD: Double,
    val HRK: Double,
    val HUF: Double,
    val IDR: Double,
    val ILS: Double,
    val INR: Double,
    val ISK: Double,
    val JPY: Double,
    val KRW: Double,
    val KZT: Double,
    val MVR: Double,
    val MXN: Double,
    val MYR: Double,
    val NOK: Double,
    val NZD: Double,
    val PAB: Int,
    val PEN: Double,
    val PHP: Double,
    val PKR: Double,
    val PLN: Double,
    val PYG: Double,
    val RON: Double,
    val RUB: Double,
    val SAR: Double,
    val SEK: Double,
    val SGD: Double,
    val THB: Double,
    val TRY: Double,
    val TWD: Double,
    val UAH: Double,
    val USD: Int,
    val UYU: Double,
    val ZAR: Double
)

Advanced Features

Let us consider one more example

JSON File:

Check on advanced options:

Resultant Kotlin class:

Hence any valid formatted JSON can be converted into Kotlin data class very easily by using the amazing “JSONToKotlin” plugin. Very helpful for your development. Advanced properties help to convert JSON to kotlin with the available options and hence it is very useful in the development.


Article Tags :