JSON Processing in Kotlin: Libraries, Parsing, and Serialization

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

JSON Processing in Kotlin: Complete Guide



JSON (JavaScript Object Notation) remains one of the most popular data exchange formats in web development, mobile applications, and microservices. Kotlin, being a modern language for JVM, Android, and multiplatform projects, offers several powerful tools for working with JSON. In this article, we will thoroughly examine the main libraries, their capabilities, and best practices.



Why do you need a JSON library in Kotlin?

Although Kotlin runs on JVM and can use Java libraries, native Kotlin solutions (e.g., kotlinx.serialization) provide:

  • Type safety at compile time
  • Support for data classes and sealed classes
  • Multiplatform capability (KMP)
  • Minimal boilerplate code


Main libraries for working with JSON in Kotlin



1. kotlinx.serialization (recommended)

This is the official JetBrains library, built into the Kotlin ecosystem. It supports multiplatform and does not require reflection.

Setup (Gradle):

plugins {    kotlin("jvm") version "1.9.22"    kotlin("plugin.serialization") version "1.9.22"}

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


JSON parsing example:

import kotlinx.serialization.*import kotlinx.serialization.json.*

@Serializabledata class User( val id: Int, val name: String, val email: String, @SerialName("is_active") val isActive: Boolean = false)

fun main() { val jsonString = """ { "id": 1, "name": "Ivan Petrov", "email": "ivan@example.com", "is_active": true } """.trimIndent()

val user = Json.decodeFromString<User>(jsonString) println("User: ${user.name}, active: ${user.isActive}")

// Serialization back to JSON val jsonOutput = Json.encodeToString(user) println(jsonOutput)}


2. Gson (by Google)

A popular Java library that easily adapts to Kotlin. Suitable for simple projects without multiplatform support.

Example:

import com.google.gson.Gsonimport com.google.gson.annotations.SerializedName

data class Product( @SerializedName("product_id") val id: Int, val title: String, val price: Double)

fun main() { val gson = Gson() val json = """{"product_id": 101, "title": "Laptop", "price": 79999.99}""" val product = gson.fromJson(json, Product::class.java) println("Product: ${product.title}, price: ${product.price} rub.") // Reverse serialization val jsonOut = gson.toJson(product) println(jsonOut)}


3. Moshi (by Square)

A modern alternative to Gson with better performance and support for Kotlin code generation.

Example:

import com.squareup.moshi.Moshiimport com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactoryimport com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory

data class Book( val isbn: String, val author: String, val year: Int)

fun main() { val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() val adapter = moshi.adapter(Book::class.java) val json = """{"isbn": "978-5-17-123456-7", "author": "Leo Tolstoy", "year": 1877}""" val book = adapter.fromJson(json) println("Book: ${book?.author} - ISBN: ${book?.isbn}")}


Library comparison

  • kotlinx.serialization — the best choice for new projects, multiplatform, and Android. Requires a compiler plugin.
  • Gson — easy to use, suitable for quick prototypes

Recommendations