Converting JSON to Data Class in Kotlin: A Comprehensive Guide
In the realm of modern programming, JSON (JavaScript Object Notation) has become a ubiquitous data-interchange format. Kotlin, a powerful and expressive programming language, provides seamless integration with JSON through its extension functions and data classes. This guide will walk you through the process of converting JSON to data class in Kotlin, making your development experience more efficient and enjoyable.
Understanding Data Classes in Kotlin
Before diving into JSON conversion, let's briefly understand Kotlin data classes. Introduced in Kotlin 1.1, data classes are used to hold data and provide convenient functions like `equals()`, `hashCode()`, and `toString()`. They are perfect for data transfer objects (DTOs) and data modeling. Here's a simple example:
data class User(val name: String, val age: Int)
JSON Library in Kotlin
Kotlin standard library provides built-in support for JSON through the `kotlinx.serialization` library. However, for this guide, we'll use the popular `Moshi` library by Square. First, add the dependency to your `build.gradle` (Module) file:

implementation 'com.squareup.moshi:moshi-kotlin:1.12.0'
Defining Data Classes for JSON
To convert JSON to data class, first, you need to define a data class that matches the JSON structure. Let's consider the following JSON:
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
Here's the corresponding data class:
data class User(
val name: String,
val age: Int,
val address: Address
)
data class Address(
val street: String,
val city: String
)
Parsing JSON to Data Class
Now that we have our data classes, let's parse the JSON using Moshi:

- First, create an instance of `Moshi` and a `JsonAdapter` for our `User` data class.
- Then, use the `fromJson()` function to parse the JSON string into a `User` object.
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val userAdapter = moshi.adapter(User::class.java)
val json = "{\"name\": \"John Doe\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"Anytown\"}}"
val user = userAdapter.fromJson(json)
Serializing Data Class to JSON
Moshi also allows you to serialize data classes to JSON. Here's how you can do it:
val json = userAdapter.toJson(user)
println(json)
Handling Nested JSON Objects and Lists
Moshi can handle nested JSON objects and lists seamlessly. Here's an example with a list of `User` objects:
data class Users(val users: List)
val json = "[{\"name\": \"John Doe\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"Anytown\"}}, {\"name\": \"Jane Doe\", \"age\": 28, \"address\": {\"street\": \"456 Oak St\", \"city\": \"Anytown\"}}]"
val usersAdapter = moshi.adapter(Users::class.java)
val users = usersAdapter.fromJson(json)
Conclusion
In this guide, we've explored how to convert JSON to data class in Kotlin using the Moshi library. By defining data classes that match the JSON structure and using Moshi's `fromJson()` and `toJson()` functions, you can seamlessly work with JSON data in your Kotlin applications.






















