jsondecode.com logo

JSON in Kotlin — kotlinx.serialization & Gson

Kotlin has two popular approaches: kotlinx.serialization (the official Kotlin multiplatform library) and Gson (from Google). For new projects, kotlinx.serialization is recommended.

Parse JSON with kotlinx.serialization

Add the @Serializable annotation and use Json.decodeFromString<T>().

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

@Serializable
data class User(val id: Int, val name: String, val email: String)

val json = """{"id":1,"name":"Alice","email":"alice@example.com"}"""
val user = Json.decodeFromString<User>(json)
println(user.name) // Alice

Serialize to JSON

Use Json.encodeToString() to convert a data class to a JSON string.

val user = User(id = 1, name = "Alice", email = "alice@example.com")

val json = Json.encodeToString(user)
// {"id":1,"name":"Alice","email":"alice@example.com"}

// Pretty-print
val prettyJson = Json { prettyPrint = true }
println(prettyJson.encodeToString(user))

Parse JSON with Gson

Gson is simpler for Android projects already using it. Add com.google.code.gson:gson.

import com.google.gson.Gson

data class User(val id: Int, val name: String, val email: String)

val gson = Gson()

// Deserialize
val user = gson.fromJson(json, User::class.java)

// Serialize
val output = gson.toJson(user)

Handle JSON parse errors in Kotlin

kotlinx.serialization throws SerializationException. kotlinx.serialization is type-safe so most errors occur at compile time, but malformed JSON still throws at runtime.

import kotlinx.serialization.SerializationException

try {
    val user = Json.decodeFromString<User>("""{"id": "bad"}""")
} catch (e: SerializationException) {
    println("Parse error: ${e.message}")
}

Frequently Asked Questions

How do I parse JSON in Kotlin?

With kotlinx.serialization: annotate your data class with @Serializable and use Json.decodeFromString<MyClass>(json). With Gson: gson.fromJson(json, MyClass::class.java).

What is the recommended JSON library for Kotlin?

kotlinx.serialization is the official Kotlin multiplatform library and works with Android, JVM, and native targets. For Android-only projects, Gson or Moshi are also common choices.

How do I handle nullable fields in Kotlin JSON parsing?

Declare the property as nullable (String?) in your data class. kotlinx.serialization and Gson both map JSON null to Kotlin null automatically.

How do I pretty-print JSON in Kotlin?

With kotlinx.serialization: create a Json instance with prettyPrint = true: val prettyJson = Json { prettyPrint = true }; prettyJson.encodeToString(obj). With Gson: GsonBuilder().setPrettyPrinting().create().

How do I parse a JSON array in Kotlin?

With kotlinx.serialization: Json.decodeFromString<List<MyClass>>(json). With Gson: use TypeToken: val type = object : TypeToken<List<MyClass>>() {}.type; gson.fromJson(json, type).

Format and validate your JSON instantly

Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.

Open JSON Formatter →

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.