Introduction: Why JSON in Kotlin is Both Pain and Joy
\nIf you've ever written code for Android or backend in Kotlin, you've certainly encountered JSON. It's like air for a modern developer: APIs, configs, databases — everything speaks this language. But here's the paradox: the question "How to parse JSON in Kotlin?" on Stack Overflow gets thousands of views every month. Why? Because in Kotlin, unlike Java, there is null safety, and old libraries like Gson start to "act up."
\nIn this tutorial, we'll cover parsing JSON in Kotlin from simple to complex. You'll learn how to work with Kotlinx Serialization, Moshi, and the classic Gson. We'll write real code that you can immediately copy into your project. Let's go!
\n\n\n\n
What is JSON and Why Doesn't Kotlin Befriend It Immediately?
\nJSON (JavaScript Object Notation) is a text format for data exchange. It looks something like this:
\n{\n "name": "Kotlin",\n "year": 2011,\n "features": ["null safety", "coroutines"]\n}\nIt seems simple, right? But Kotlin has strong typing and null safety. When you parse JSON, you must explicitly specify which fields can be null and which are mandatory. If the library doesn't account for this, you'll get a NullPointerException or strange bugs.
That's why choosing the right library for parsing JSON in Kotlin is not just a matter of taste, but a question of project stability.
\n\nTop 3 Libraries for Parsing JSON in Kotlin
\nWe'll look at three main approaches. Each has its pros and cons.
\n- \n
- Kotlinx Serialization — the official library from JetBrains. Best integration with the language. \n
- Moshi — a modern library from Square. Works with both Java and Kotlin. \n
- Gson — the good old standard. Still popular, but requires caution with Kotlin. \n
1. Kotlinx Serialization — The Pro's Choice
\nThis is the native library for Kotlin. It uses code generation and fully supports null safety. If you're starting a new Kotlin project, this is your choice.
\nInstallation: Add the plugin to build.gradle.kts:
plugins {\n kotlin("plugin.serialization") version "1.9.0"\n}\n\ndependencies {\n implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")\n}\nCode Example:
\nimport kotlinx.serialization.Serializable\nimport kotlinx.serialization.json.Json\n\n@Serializable\ndata class User(\n val name: String,\n val year: Int,\n val features: List<String>\n)\n\nfun main() {\n val jsonString = """\n {\n "name": "Kotlin",\n "year": 2011,\n "features": ["null safety", "coroutines"]\n }\n """.trimIndent()\n\n val user = Json.decodeFromString<User>(jsonString)\n println(user.name) // Kotlin\n println(user.features[0]) // null safety\n}\nNote: Kotlinx Serialization automatically picks up types. If a field is missing in JSON and is not marked as nullable in the data class, you'll get an exception. This is good — you won't miss an error.
2. Moshi — Flexible and Fast
\nMoshi is a library from Square that was created with Kotlin in mind. It also supports code generation and null safety. Unlike Gson, Moshi does not use reflection by default, making it faster.
\nInstallation:
\ndependencies {\n implementation("com.squareup.moshi:moshi-kotlin:1.15.0")\n ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.0")\n}\nCode Example:
\nim