How to convert a JSON into usable data in Kotlin

Oscar de la Hera Gomez
Two flowers that represent Kotlin and Android Studio side by side. Beneath them sits the text "Convert JSON to Data."

A step by step tutorial on converting a JSON into usable data in Android Studio using Kotlin.

Step One: Create the JSON

A screenshot of the pop up that appears in Android Studio when you create a new file. Highlighted is the area where you enter the name. We have written sample.json. Write the name of the file that you wish and press enter to create the file.

Create a json file using the tutorial linked below.

Step Two: Create the Data Class File

A screenshot of Android Showing the newly created UIContent file.

In the models folder, create a new file named after the class that you wish to associate with your json.

In our case, we have called it UIContent.kt, as we will use it to read a language JSON that will be used to populate UI strings.

Step Three: Create JSON Data Class

A screenshot of Android Studio showing the populated UIContent File (Code available Below) side by side with the JSON it mirrors.

In the file created in Step Two, create a data class that matches the JSON you wish to convert into usable data.

We have provided a sample of how to do this below.

Step Four: Create the convert JSON to string functionality

A screenshot of Android Studio showing the ReadJSON utility.

Follow the tutorial below to implement the functionality required to read the JSON and convert it to a string.

Step Five: Add Gson to project

A screenshot of Android Studio showing the build.gradle.kts (Module :app) open. Highlighted is the Gson dependency that we have added to the app. Code available below.

In order to convert the JSON string into usable data, we need to use the Gson library.

Navigate to your app level build gradle (i.e. build.gradle.kt (Module :app)) and add the following line to the dependencies:

implementation("com.google.code.gson:gson:2.8.9")

Please note that version 2.8.9 was the latest version available when we wrote this tutorial but may not be the latest version available when you implement this. Please update this string to use the latest version available.

Step Six: Sync the Gradle Files

A screenshot of Android Studio showing a gradle file that has recently been modified. Highlighted is the "Sync Now" button that appears on the right of the prompt at the top of the Gradle file. Click this button to sync the gradle files with the project.

Sync the Gradle files to make sure the Gson library registers.

Step Seven: Convert the JSON String to Data

A screenshot of Android Studio showing you how to convert a JSON string into usable data. Code available below.

To convert the JSON string gathered from the function created in Step Four into the data class created in Step Three, use code similar to the one below:

val data = Gson().fromJson(jsonString, UIContent::class.java)

Step Eight: Implement Data

A screenshot of Android Studio showing how we use the data. Examples listed below.

To use the data, use dot notation with parameters that match the class that you created in Step Three (i.e. data.sample.sampleString or data.sampleTwo.sampleB.aSampleString).

Step Nine: Test

A screenshot of an Android Device showing the data printed on the screen.

Run the app on a device and confirm that the JSON is read and the data is processed.

Verification can be done by printing to a Log or printing to the screen, as we have done in this tutorial.

Looking to learn how to make an Android localization manager ?

To learn our design system for localizing apps into multiple languages, follow the tutorial below.

Warning: Data classes cannot be obfuscated

To guarantee that the app functions as expected on release, you should add ANY JSON data classes to the projects proguard-rules.pro file.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details