Working with JSON in Kotlin: Parsing and Serialization for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with JSON in Kotlin: Parsing and Serialization for Beginners



JSON (JavaScript Object Notation) is the most popular data exchange format in modern web applications and APIs. If you have started learning Kotlin, sooner or later you will encounter the need to convert JSON strings into objects of your application (deserialization or parsing) and back (serialization).



In this article, we will cover the main ways to work with JSON in Kotlin using three popular libraries: kotlinx.serialization, Gson, and Moshi. You will learn how to quickly start parsing JSON and serializing data without unnecessary complexity.



What are JSON Parsing and Serialization?



Before moving to the code, let's define the terms:



  • Serialization is the process of converting an object (e.g., a Kotlin data class) into a JSON format string.
  • Parsing (deserialization) is the reverse process: from a JSON string we obtain an object of a specific type.


Interaction with JSON in Kotlin can be organized in several ways. There is no built-in JSON support in the language, so we use third-party libraries.



Library kotlinx.serialization (Recommended Approach)



kotlinx.serialization is the official library from JetBrains that integrates perfectly with the Kotlin language. It supports multiplatform (JVM, JS, Native) and works at compile time, making it very fast and safe.



Installation



Add the plugin to the build.gradle.kts file:



plugins {    kotlin("plugin.serialization") version "1.9.0"}

dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")}


Example: JSON Parsing



Suppose we have JSON with user data:



{  "name": "Ivan",  "age": 28,  "email": "ivan@example.com"}


Let's create a data class and annotate it with @Serializable:



import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Json

@Serializabledata class User( val name: String, val age: Int, val email: String)

fun main() { val jsonString = """ {"name": "Ivan", "age": 28, "email": "ivan@example.com"} """.trimIndent()

val user = Json.decodeFromString<User>(jsonString) println(user) // User(name=Ivan, age=28, email=ivan@example.com)}


Example: Serialization to JSON



fun main() {    val user = User(name = "Anna", age = 25, email = "anna@example.com")    val jsonString = Json.encodeToString(user)    println(jsonString)    // {"name":"Anna","age":25,"email":"anna@example.com"}}


Advantages: type safety, speed, no reflection, support for nullable fields, default values, and nested objects.



Library Gson (from Google)



Gson is one of the oldest and most popular libraries for working with JSON in the JVM ecosystem. It is simple to use but uses reflection, which is slightly slower than kotlinx.serialization.



Installation



dependencies {    implementation("com.google.code.gson:gson:2.10.1")}


Example: JSON Parsing



import com.google.gson.Gson

data class Product( val id: Int, val title: String, val price: Double)

fun main() { val gson = Gson() val jsonString = """{"id": 101, "title": "Laptop", "price": 75000.0}"""

val product = gson.fromJson(jsonString, Product::class.java) println(product) // Product(id=101, title=Laptop, price=75000.0)}


Example: Serialization to JSON



fun main() {    val product = Product(id = 202, title = "Mouse", price = 2                

Blogs

Book Recommendations