JSON Processing in Kotlin: Libraries, Tools, and Code Examples
JSON (JavaScript Object Notation) is one of the most popular data exchange formats in modern web development. When creating applications in Kotlin, whether for Android, backend, or desktop, you almost always need to parse and generate JSON. In this article, we will thoroughly examine the main libraries for working with JSON in Kotlin, their capabilities, advantages, and provide practical code examples.
Why is separate JSON processing needed in Kotlin?
Kotlin is a statically typed language that runs on top of the JVM (as well as Native and JS). Unlike dynamic languages, where JSON can be converted to a dictionary "on the fly," Kotlin typically works with typed objects. This increases code reliability and allows the IDE to automatically suggest fields. Serialization/deserialization libraries are used to convert JSON to Kotlin objects (data classes) and back.
Main Libraries for JSON in Kotlin
Let's look at the three most popular libraries: kotlinx.serialization (recommended by JetBrains), Gson (from Google), and Moshi (from Square).
1. kotlinx.serialization
This is the official serialization library for Kotlin, supported by JetBrains. It works on a multiplatform basis (JVM, Native, JS) and does not use reflection, which provides high performance and type safety.
Installation: Add the plugin and dependency in build.gradle.kts:
plugins { kotlin("plugin.serialization") version "1.9.22"}
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")}Usage example:
import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.encodeToStringimport kotlinx.serialization.decodeFromString
@Serializabledata class User(val name: String, val age: Int, val email: String? = null)
fun main() { val json = Json { prettyPrint = true; ignoreUnknownKeys = true }
// Serialization (object -> JSON) val user = User("Alexey", 30, "alex@example.com") val jsonString = json.encodeToString(user) println(jsonString)
// Deserialization (JSON -> object) val input = """{"name":"Maria","age":25}""" val parsedUser = json.decodeFromString<User>(input) println(parsedUser)}Features:
- Support for nullable fields (like
emailin the example). - Configuration via DSL:
prettyPrint,ignoreUnknownKeys, and others. - Works with enums, sealed classes, nested objects.
- Multiplatform — one code for Android, iOS, web.
2. Gson
Gson is a classic library from Google, widely used in the Java world. It also works in Kotlin, but requires caution with nullable types and data classes.
Installation:
dependencies { implementation("com.google.code.gson:gson:2.10.1")}Example:
import com.google.gson.Gsonimport com.google.gson.GsonBuilder
data class Product(val id: Int, val title: String, val price: Double)
fun main() { val gson: Gson = GsonBuilder().setPrettyPrinting().create()
// Serialization val product = Product(1, "Laptop", 89999.99) val json = gson.toJson(product) println(json)
// Deserialization val jsonString = """{"id":2,"title":"Mouse","price":1500.50}""" val parsed = gson.fromJson(jsonString, Product::class.java) println(parsed)}Important: Gson uses reflection, so for Kotlin data classes, @JvmOverloads or configuration may be required.