How to read a JSON file from the assets folder using Kotlin

A step by step guide on reading a JSON file from the assets folder of an Android Studio project and converting it to a string in Kotlin.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/13/2023 at 15:05
Last Updated on 06/14/2023 at 11:27
Two flowers that represent Kotlin and Android Studio. Beneath them sits the text "Read JSON."

A step by step guide on reading a JSON file from the assets folder of an Android Studio project and converting it to a string in Kotlin.

SubscribeDownload Open Source Kotlin Starter Project

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.

Follow the tutorial below to create a JSON that can be read by an Android app.

How to add a JSON file to an Android Studio project

Step Two: Create Read JSON Utility

A screenshot of Android Studio showing the ReadJSON utility.

In your project, create a new file called ReadJSONFromAssets.kt and input the code below.

We suggest you place this in a utilities folder, but you can also opt to not create a file and place the function below wherever you wish in your project.

Step Three: Read the JSON

A screenshot of Androids Studio showing the ReadJSON utility being called and it successfully finding a file. This success is shown on the LogCat logs.

Call ReadJSONFromAssets by passing an activity or composable's context and the name of the JSON file.

Looking to convert the JSON into usable data?

To learn how to convert a JSON into usable data in Kotlin and Android Studio follow the tutorial below.

How to convert a JSON into usable data in Kotlin

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe