CodeGym /Courses /Kotlin SELF /Collections and Map in JSON

Collections and Map in JSON

Kotlin SELF
Level 48, Lesson 0
Available

1. Introduction

When you first see that Json.encodeToString() “sort of by itself” produces nice JSON, you get the feeling the problem is solved forever. But serialization is not just “save to a file.” It is also a contract: what the data format will look like, whether another program can understand it, whether you yourself can read it a month later without crying, and how painful it will be to change the format later.

JSON is a language of communication, not just a “storage string.” And JSON has its own rules: it supports arrays [...], objects {...}, strings, numbers, true/false, and null. Kotlin, as you know, is richer in types: we have lists, sets, maps, nullable types, data class, and much more. So today our task is to learn to foresee in advance what exactly a Kotlin collection will turn into in JSON, and where the traps are.

Mappings: Kotlin collections and JSON shapes

Before writing code, it is useful to build a mapping table “in your head.” It is like a translator: from Kotlin to JSON.

Kotlin type What it means “semantically” What it almost always turns into in JSON
List<T>
ordered sequence, duplicates allowed JSON array [...]
Set<T>
unique elements, order usually does not matter JSON array [...] (yes-yes, still an array)
Map<String, V>
key → value, keys are strings JSON object { "key": value }
Map<K, V> where K is not String key → value, but keys are “not strings” most often still a JSON object, but keys become strings (and this is where nuances begin)

About Map, it is important to remember one basic thing: it is a collection of key-value pairs, keys are unique, values may repeat. Kotlin separately emphasizes that Map is a special collection type that stores pairs and provides access by key.

Now let’s go through each case separately, with short and clear examples.

3. List and Set in JSON: arrays

List<T> in JSON: an array [...]

Lists are the most JSON-friendly type. If you have a List<String>, almost any person (and any system) will easily understand a JSON array of strings. A list stores elements in order, indices start at zero, index-based access is possible—this is an important part of the List contract.

Let’s imagine that in our educational mini-app (let it be “BudgetBuddy,” an expense tracker) we want to store tags for an expense: food, coffee, taxi. Semantically, tags are often more convenient to keep as a list.

Example 1: List<String> turns into a JSON array

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class Tags(val items: List<String>)

fun main() {
    val json = Json { prettyPrint = true }
    println(json.encodeToString(Tags(listOf("kotlin", "json"))))
    // {
    //     "items": [
    //         "kotlin",
    //         "json"
    //     ]
    // }
}

Everything here is as predictable as it gets: the list became an array. And this is exactly the case where serialization works “as you expected.”

Set<T> in JSON: also an array

With Set, the first mild cognitive dissonance begins: in Kotlin we clearly distinguish List and Set. A Set stores only unique elements—that is its core idea. Kotlin documentation explicitly highlights: Set stores unique elements, order in the general case is not guaranteed, and even null can appear only once.

But JSON does not have a separate “set” type. So serialization usually does the most logical thing: turns a Set into a JSON array. That is, in shape the JSON will be the same as for a List.

And this is important: uniqueness is not a property of JSON. Uniqueness is your agreement (and the logic of your Kotlin code).

Example 2: Set<String> also becomes an array

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class UniqueTags(val items: Set<String>)

fun main() {
    val json = Json { prettyPrint = true }
    println(json.encodeToString(UniqueTags(setOf("food", "coffee"))))
    // {
    //     "items": [
    //         "food",
    //         "coffee"
    //     ]
    // }
}

Visually, it is just a regular array. So if you later send this JSON “outside” (for example, to another system), it will not know it is a set unless you say so in words or documentation.

4. Map<String, V> in JSON: an object {...}

Now we move on to maps (Map). The meaning of Map is to store “key → value” pairs, with unique keys, and access by key as the primary operation.

A JSON object {...} by nature is also “key → value,” where keys are strings. Therefore, Map<String, V> fits JSON almost perfectly: you get a regular object where field names are the map keys.

For our BudgetBuddy, this is very useful when we want to store, for example, the total spend by category: "food" -> 1200, "taxi" -> 450.

Example 3: Map<String, Int> turns into a JSON object

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class Counters(val byName: Map<String, Int>)

fun main() {
    val json = Json { prettyPrint = true }
    val data = Counters(mapOf("apples" to 2, "bananas" to 5))
    println(json.encodeToString(data))
    // {
    //     "byName": {
    //         "apples": 2,
    //         "bananas": 5
    //     }
    // }
}

It looks “human-friendly,” compact, and quick to read. That is exactly why Map<String, V> is a frequent guest in JSON.

5. Non-string Map keys: what will happen in JSON

In JSON, object keys are always strings. Even if they look like numbers, they are still strings (for example, "10"). So if you serialize a Map<Int, Int>, then “in JSON shape” it will still be an object {...}, and its keys will become strings.

Sometimes this looks harmless. And sometimes it breaks an integration, because someone expects “numeric keys,” but those simply do not exist in a JSON object.

Example 4: Map<Int, Int> — keys become strings "10", "20"

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class IntKeyCounters(val byId: Map<Int, Int>)

fun main() {
    val json = Json { prettyPrint = true }
    val data = IntKeyCounters(mapOf(10 to 3, 20 to 7))
    println(json.encodeToString(data))
    // {
    //     "byId": {
    //         "10": 3,
    //         "20": 7
    //     }
    // }
}

Notice: in Kotlin the keys were 10 and 20 (numbers), but in JSON they became "10" and "20" (strings).

If you read this back into Kotlin as the same Map<Int, Int>, the library can usually parse string keys into Int. But if this JSON ends up in someone else’s system, and there the key "010" is different from "10", or keys are not always numbers—then the fun begins.

6. When Map becomes a bad format

Map in Kotlin is a convenient structure. But convenience “for code” and convenience “for a data format” are different things. Kotlin clearly shows that Map is a set of pairs, and the order of pairs is not part of the meaning (two maps are equal regardless of pair order).

Now imagine typical problematic situations:

You want the key to be not a string but a complex object (for example, data class CategoryKey(val name: String, val year: Int)). In Kotlin this is possible, but you cannot put such a key into a JSON object “nicely.”

You want the key to be an Int, but it has an important format, for example "00123" as a “code,” where leading zeros are significant. In Kotlin you would store it as a string, but if you decide to store it as an Int, then when serializing back you will no longer be able to distinguish "00123" from "123".

You want to extend the format. Today one number is enough for the “key → value” pair; tomorrow you need to add a comment, a date, an author. Map starts resisting because the value becomes complex, and the format sometimes becomes less obvious.

In such cases it is often better to make the format more explicit, even if it is a bit longer.

7. Alternative: “map as a list of entries” List<Entry>

If JSON is more important than “one-line prettiness,” then instead of Map<K, V> people often use a list of objects like:

[
  {"key": 10, "value": 3},
  {"key": 20, "value": 7}
]

It is longer, but:

  • the key remains a number as a value, rather than being hidden in a string key of a JSON object;
  • the format is easier to extend (you can add fields);
  • it is easier to validate and document.

Example 5: a list of entries instead of Map<Int, Int>

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class IntCounterEntry(val key: Int, val value: Int)

@Serializable
data class IntCounterList(val items: List<IntCounterEntry>)

fun main() {
    val json = Json { prettyPrint = true }
    val data = IntCounterList(listOf(IntCounterEntry(10, 3), IntCounterEntry(20, 7)))
    println(json.encodeToString(data))
}

JSON will look roughly like this (logically important; we are not nitpicking about indentation):

{
  "items": [
    { "key": 10, "value": 3 },
    { "key": 20, "value": 7 }
  ]
}

Yes, more text. But the format is “self-describing”: you can immediately see that key is a number and value is a number.

8. Example: an expense report as collections + Map

So the examples do not hang in a vacuum, let’s tie this to our hypothetical BudgetBuddy. Suppose we have a list of expenses, and we want to build a report: the total amount by category.

The expense list is a List<Expense>, and it is convenient to keep the final report in a Map<String, Int> (category → total). Here, Map fits perfectly semantically, because the category is a string, and the JSON object comes out naturally.

Example 6: the “expenses + totals by category” model

import kotlinx.serialization.Serializable

@Serializable
data class Expense(val title: String, val category: String, val amount: Int)

@Serializable
data class Report(val expenses: List<Expense>, val totalByCategory: Map<String, Int>)

Here we deliberately chose:

  • expenses: List<Expense> — a sequence of records (natural for a file).
  • totalByCategory: Map<String, Int> — a summary with string keys, so the JSON object will be correct and readable.

9. How to build a Map from a list without “magic”

Since we started talking about a report, a practical question comes up almost immediately: “OK, but how do we build a Map<String, Int> from the list of expenses?”

You could write a loop and accumulate totals by hand. But Kotlin collections provide many tools for transformations. For example, maps have convenient operations mapKeys() and mapValues()—when you want to transform keys or values.

For now we will do the simplest and most readable version—using a plain for (it is easier for beginners to read), and in later days you will write this more functionally.

Example 7: accumulating totals in a MutableMap

fun buildTotals(expenses: List<Expense>): Map<String, Int> {
    val totals = mutableMapOf<String, Int>()

    for (e in expenses) {
        val old = totals[e.category] ?: 0
        totals[e.category] = old + e.amount
    }

    return totals
}

Notice the style: totals[e.category] ?: 0 is the classic pattern: “if there is no value yet, assume it was 0.”

10. Converting List<Entry> and Map

Sometimes, by format, it is more beneficial to store a “list of entries,” but in code you want to work with a Map. Then you have to convert back and forth.

Kotlin offers several ways to build a Map from a collection. One of them is associate(): you return a Pair(key, value) for each element, and Kotlin collects a map from it. The documentation describes associate() and emphasizes that it builds a Map from Pair.

Example 8: list of entries → Map

fun asMap(items: List<IntCounterEntry>): Map<Int, Int> {
    return items.associate { it.key to it.value }
}

Example 9: Map → list of entries

fun asList(map: Map<Int, Int>): List<IntCounterEntry> {
    return map.map { (k, v) -> IntCounterEntry(k, v) }
}

It is important to understand: Map is not just a “list of pairs”; it has its own properties (uniqueness of the key and access by key). So you choose a structure depending on what matters more: convenience in code or clarity/compatibility of the format.

11. How to choose: Map or List<Entry>

When you design JSON, it is convenient to keep a small selection rule in mind. Not as an “absolute truth,” but as a prompt for common sense.

flowchart TD
    A["Need to preserve the key → value mapping"] --> B{Are the keys strings?}
    B -- "Yes" --> C["You can use Map⟨String, V⟩ → JSON object"]
    B -- "No" --> D{Are the keys definitely numbers and everyone is OK with that?}
    D -- "Yes" --> E["Map⟨Int, V⟩ is possible, but keys will become strings"]
    D -- "No / not sure" --> F["Better use List⟨Entry⟩ with key/value fields"]

And yes: sometimes the right answer is “not sure.” In programming, that is a perfectly valid position, especially before the first integration with someone else’s service.

12. Common mistakes

Mistake #1: expecting Set to be preserved as a “set” in JSON.
It is very tempting to think that if it is a Set in Kotlin, then JSON will also have something “special.” But JSON is a simple format: it has no separate “set” type, so Set turns into an array, just like List. Element uniqueness is your logic, not a property of the format. If it is critical, you must explicitly enforce uniqueness when adding and when reading data.

Mistake #2: using Map<Int, ...> and forgetting that keys in JSON will be strings.
The problem is not that the serialization is “bad”—it does the only possible thing because JSON object keys are strings. The problem is expectations: today you look at JSON and see "10": 3, and tomorrow someone else (or you) starts treating it as a string key where leading zeros, spaces, and special formats are possible. If keys are truly “numeric by meaning” and the format matters, it is often easier to switch to a list of entries.

Mistake #3: making a Map with a “complex key” and hoping for “pretty JSON.”
In Kotlin, a map key can be almost anything, but a JSON object cannot: keys must be strings. Therefore, object keys almost always lead to an inconvenient or surprising format. In such cases it is better to create an explicit model: List<Entry>, where the key is a separate field, and it can be a complex object (because a JSON value can be an object, but a key cannot).

Mistake #4: choosing a structure “because it looks nicer,” not because it can be unambiguously read back.
This is a very common beginner trap: “A Map looks compact, so let’s use it.” And then suddenly it turns out that reading it back requires a bunch of caveats: what keys are allowed, whether they can be parsed as numbers, what to do with empty strings, and so on. A good criterion is to ask yourself: “Can I, six months from now, write the decode without hints and not make mistakes?” If you are unsure, make the format more explicit.

Mistake #5: mixing “a convenient storage format” and “a convenient in-code format” into one pile.
In code, it is often convenient to have a Map (fast lookup by key), and in a file it is convenient to have a List (explicit order, extensibility, typed fields). It is normal to keep conversion “to file” and “from file” as a separate step. Kotlin collections are good precisely because between List and Map you can usually convert in a couple of lines via associate() and map { ... }.

1
Task
Kotlin SELF, level 48, lesson 0
Locked
Note tags
Note tags
1
Task
Kotlin SELF, level 48, lesson 0
Locked
Unique tags
Unique tags
1
Task
Kotlin SELF, level 48, lesson 0
Locked
Expense report
Expense report
1
Task
Kotlin SELF, level 48, lesson 0
Locked
Counters by ID
Counters by ID
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION