Generate Kotlin Data class from Json using JsonToKotlin Class Plugin
Tags: jsontokotlinclass, androidplugin, kotlindataclass, jsontokotlin
Published: 2022-09-04 09:43:52

---
title: Generate Kotlin Data class from Json using JsonToKotlin Class Plugin
published: true
date: 2022-07-15 16:22:08 UTC
tags: jsontokotlinclass,androidplugin,kotlindataclass,jsontokotlin
canonical_url: https://medium.com/@jayknow/generate-kotlin-data-class-from-json-using-jsontokotlin-class-plugin-c568adcd2e2e
---

![Json to Kotlin Data Class using JsonToKotlinClass plugin](https://cdn-images-1.medium.com/max/800/1*FyQsPk_VdUam-TJaAFXyOQ.png)

While we integrate API, We have to create Kotlin classes. I am sure most of you search on google “Json to Kotlin class generate online” or For Simple JSON data, you have created Kotlin Classes manually.

We need someone to do this tedious task for us. Here we have JsonToKotlin Class plugin for it.

**JsonToKotlinClass** is a plugin to create Kotlin data classes from JSON.

Supporting all kinds of JSON libs’ annotation(Gson, Jackson, Fastjson, Moshi and LoganSquare, kotlinx.serialization).

if you want to read more about plugin features: [https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-](https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-)

Now we will follow two steps to generate Kotlin data class from JSON:  
Step 1: Install JsonToKotlinClass Plugin in Android Studio  
Step 2: Convert JSON data into Kotlin Class

Let’s start.

**Step 1: Install JsonToKotlinClass Plugin**

- Open the **Preferences** window by clicking **File > Settings** (on Mac, **Android Studio > Preferences** ).

1. Click on  **Plugins**
2. Select **MarketPlace** and Search **“JsonToKotlin”**
3. Click ‘ **Install ** ’
4. Click **Apply** and **Ok** to save your changes

![Follow Above Steps to Install JsonToKotlinClass Plugin](https://cdn-images-1.medium.com/max/1024/1*b_NR948bCLHr6H1r-fxiWQ.png)

**Step 2: Convert JSON data into Kotlin Class**

Now it’s time to generate Kotlin Data classes from JSON. Here I use

JSONPlaceholder API, JSONPlaceholder is a free online REST API.

- Select the Project section, then right-click on that folder and select **New > Kotlin data class file from JSON** (as shown in the image below).

![](https://cdn-images-1.medium.com/max/1024/1*kqSSyklK3pMfe2HakXWQxg.png)

- In Generate Kotlin Data Class Code Dialog, Past JSON String in Blank Area and give Class name “ **TodoList** ” and click **Generate**.

![Past Json and click generate](https://cdn-images-1.medium.com/max/1024/1*f3n8k2flzNUwsGjWNC-v_w.png)

- We can see two files generated as shown in the image below.

![Two files were generated](https://cdn-images-1.medium.com/max/1024/1*t1WRg07366Wqfa5pEEBeRw.png)

- Customize the **Advance** feature as below

1. In Property, select the **Val** keyword and select Type **Non-nullable.**
2. In Annotation, select  **Gson.**
3. In Extensions, select **Add Gson @Expose Annotation.**

Apply the above features to the below JSON:

![UserItem JSON String](https://cdn-images-1.medium.com/max/475/1*SqSCl-cIAUWQ3A2fLn38vg.png)

Three files were generated as the below image:

![UserItem, Address and Geo Class were Generated](https://cdn-images-1.medium.com/max/1024/1*bYIzgm8h9pmMics1mh95GA.png)

That’s it.

Thanks for spending your time reading this article. ❤️ and share if you’ve enjoyed it.