Kotlin JSON: A Guide from Zero to Pro

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

Introduction: Why JSON in Kotlin is Both Pain and Joy

\n

If 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."

\n

In 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?

\n

JSON (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}
\n

It 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.

\n

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\n

Top 3 Libraries for Parsing JSON in Kotlin

\n

We'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
\n\n

1. Kotlinx Serialization — The Pro's Choice

\n

This 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.

\n

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

\n
plugins {\n    kotlin("plugin.serialization") version "1.9.0"\n}\n\ndependencies {\n    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")\n}
\n

Code Example:

\n
import 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}
\n

Note: 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.

\n\n

2. Moshi — Flexible and Fast

\n

Moshi 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.

\n

Installation:

\n
dependencies {\n    implementation("com.squareup.moshi:moshi-kotlin:1.15.0")\n    ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.0")\n}
\n

Code Example:

\n
im                

Blogs

Book Recommendations