You can use org.json which is shipped with Android:

JSONObject(map).toString()
Answer from Miha_x64 on Stack Overflow
🌐
GitHub
github.com › Kotlin › kotlinx.serialization › issues › 746
How to convert map into json string · Issue #746 · Kotlin/kotlinx.serialization
March 9, 2020 - example val params = mutableMapOf () params.put("long", 100L) params.put("int", 10) params.put("string", "haha") params.put("map", mutableMapOf("longg" to 10L, "stringg" to "ok")) LogUtil.d("test", json.stringify(params))
Author: Kotlin
Discussions

How to map a JSON string to Kotlin Map - Stack Overflow
I have a simple Kotlin program that access a Mongo database and produce a JSON string as below; "{ "_id" : { "$oid" : "593440eb7fa580d99d1abe85"} , "name" : "Firstname Secondname" , " More on stackoverflow.com
🌐 stackoverflow.com
Turning JSON array into Map
As stated before, it would be super helpful to have a serialization package in your project, like Jackson or Kotlin Serialization. Otherwise, you would have to parse the Json map manually, which would be extremely time consuming, but at least you did it yourself. More on reddit.com
🌐 r/Kotlin
3
4
June 10, 2022
java - Kotlin: Cannot convert Map to JSON - Stack Overflow
How to convert Map to JSONObject? It accepts Map but it seems you need to perform casting. Is there any other way to do this without using library? More on stackoverflow.com
🌐 stackoverflow.com
map to json string error
Describe the bug I cannot convert map to a JSON string. To Reproduce I need to convert mapOf () to a JSON string, but running crash. env: ktor client: 2.3.3 kot... More on github.com
🌐 github.com
3
August 16, 2023
People also ask

How to pass JSON in Kotlin?
Passing JSON data in Kotlin involves creating JSON strings or objects that encapsulate the data to be transmitted or processed. Developers can pass JSON data between functions, classes, or API endpoints by serializing the data into JSON format using serialization libraries and transmitting it as needed.
🌐
dhiwise.com
dhiwise.com › post › kotlin-object-to-json-transformation-efficient-data-conversion
Mastering Kotlin Object to JSON Conversion
How to get value from JSON in Kotlin?
To extract values from JSON data in Kotlin, developers can parse the JSON string or object and access specific fields or properties using key-value lookups. By parsing the JSON structure and retrieving values based on keys, developers can access and manipulate data stored in JSON format efficiently.
🌐
dhiwise.com
dhiwise.com › post › kotlin-object-to-json-transformation-efficient-data-conversion
Mastering Kotlin Object to JSON Conversion
How to convert JSON to object in Kotlin?
To convert JSON data to objects in Kotlin, developers can utilize libraries like kotlinx.serialization or Gson to deserialize JSON strings into Kotlin object instances. By defining data classes that mirror the JSON structure and implementing deserialization logic, JSON data can be seamlessly transformed into Kotlin objects.
🌐
dhiwise.com
dhiwise.com › post › kotlin-object-to-json-transformation-efficient-data-conversion
Mastering Kotlin Object to JSON Conversion
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-json › kotlinx.serialization.json › -json-object
JsonObject | kotlinx.serialization – Kotlin Programming Language
Class representing JSON object, consisting of name-value pairs, where value is arbitrary JsonElement · Since this class also implements Map interface, you can use traditional methods like Map.get or Map.getValue to obtain Json elements.
🌐
Python Examples
pythonexamples.org › kotlin › how-to-convert-a-map-to-a-json-string
How to Convert a Map to a JSON String in Kotlin
We can convert a map to a JSON string in Kotlin using the kotlinx.serialization library's Json.encodeToString function.
🌐
DhiWise
dhiwise.com › post › kotlin-object-to-json-transformation-efficient-data-conversion
Mastering Kotlin Object to JSON Conversion
September 5, 2024 - With kotlinx.serialization, developers can deserialize JSON data back into Kotlin objects by mapping JSON key-value pairs to the corresponding properties of data classes, ensuring seamless data transformation between JSON and Kotlin structures.
Find elsewhere
🌐
BezKoder
bezkoder.com › home › kotlin – convert object to/from json string using gson
Kotlin - Convert object to/from JSON string using Gson - BezKoder
April 11, 2020 - Gson is a Java/Kotlin library for converting Java/Kotlin Objects into JSON representation, also a JSON string to an equivalent Java/Kotlin object.
🌐
Reddit
reddit.com › r/kotlin › turning json array into map
r/Kotlin on Reddit: Turning JSON array into Map
June 10, 2022 - val root = mapper.readTree(inputJson) val data = root.path("data") // Keep using path to get groups array val map = groups.associate{ it.path("id") to it.path("name") } Syntax would be different depending on your JsonObject / JsonNode library ...
🌐
DhiWise
dhiwise.com › post › how-to-streamline-data-processing-with-kotlin-json
Mastering Kotlin JSON Serialization for Efficient Data ...
July 10, 2024 - Using the kotlinx.serialization library, developers can effortlessly convert Kotlin objects to JSON and vice versa, ushering in a new era of data transformation efficiency. JSON, short for JavaScript Object Notation, is crucial in facilitating data transfer across various platforms. With Kotlin’s robust support for JSON serialization, developers can easily map data classes to JSON objects and manipulate JSON strings with ease.
🌐
BezKoder
bezkoder.com › home › kotlin – convert json to object and vice versa using jackson
Kotlin – Convert JSON to Object and vice versa using Jackson - BezKoder
December 13, 2020 - (File) // Object to Json String/File var jsonString = -> mapper.writeValueAsString(Person) -> mapper.writerWithDefaultPrettyPrinter().writeValueAsString(Person) -> mapper.writeValue(File, Person) -> mapper.writerWithDefaultPrettyPrinter().writeValue(File, Person) Let’s create a plain Class called Person in Person.kt. package com.bezkoder.kotlin.json data class Person(val name: String, val age: Int, val messages: List
Top answer
1 of 2
3

The answer provided by Bitwise DEVS may work, but the explanation is incorrect.

It is not necessary to convert the map into an immutable map - it can even be seen in the screenshot of the question: expected (Mutable)Map means that it can be mutable or immutable.

The real problem here is that the called constructor of JSONObject is defined for Map<Any?, Any?> but the given parameter is of type Map<String!, String!>. We know that String is a subtype of Any?, so why is that a problem?

It is a problem because Map is invariant in the key parameter which means that a Map<A, V> is not subtype of Map<B, V> even if A is subtype of B (and also not if it is the other way around).

The call of toMap does not work because it makes the map immutable, but because it is defined as

fun <K, V> Map<out K, V>.toMap(): Map<K, V>

In the receiver type of toMap the key parameter is annotated with the out modifier, meaning that K is covariant here. This means that a Map<Any, V> in this position can safely be considered as a supertype of Map<String, V>. Additionally, Map is always covariant in its value type, meaning that Map<Any, Any> in this position can safely be considered as a supertype of Map<String, String>.

You could as well just cast remoteMessage.data as Map<Any?, Any?> or use toMutableMap, it would be all the same. It has definitely nothing to do with mutability of the map.

Kotlin documentation on variance: https://kotlinlang.org/docs/generics.html#declaration-site-variance

Kotlin documentation of Map: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/

Definition of toMap: https://github.com/JetBrains/kotlin/blob/ea836fd46a1fef07d77c96f9d7e8d7807f793453/libraries/stdlib/src/kotlin/collections/Maps.kt#L600

2 of 2
1

I just realized the answer, if you are using Map like Map<String, String> in Kotlin there is a mutable and immutable version of it. In the question above all you need to do is to convert it to immutable Map using toMap().

val msgData = parser.fromJsonString<CommonNotification>(
            JSONObject(remoteMessage.data.toMap()).toString(),
            CommonNotification::class.java
        ) ?: return
🌐
GitHub
github.com › Kotlin › kotlinx.serialization › issues › 2411
map to json string error · Issue #2411 · Kotlin/kotlinx.serialization
August 16, 2023 - To Reproduce I need to convert mapOf<String, Any>() to a JSON string, but running crash. env: ... // ... import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json // ...
Author: Kotlin
🌐
Medium
medium.com › @shrikantjagtap99 › kotlin-map-serialization-and-deserialization-with-jackson-6343d9e56107
Kotlin Map Serialization and Deserialization with Jackson | by Shrikant Jagtap | Medium
April 26, 2020 - To fix this, we need to define a custom serializer for the map key object. Jackson provides a way to define it using @JsonDeserialize annotation and keyUsing to assign custom serializer class.
🌐
GitHub
github.com › Kotlin › kotlinx.serialization › issues › 1537
[Question] How to convert JsonObject to Map<String, Any> · Issue #1537 · Kotlin/kotlinx.serialization
June 7, 2021 - val map = Gson().fromJson(jsonString, object : TypeToken<HashMap<String, Any>>() {}.type) ref.setValue(map) Using Kotlin.Serialization results in Map<String, JsonElement>, and when sent to Firebase, each property becomes an object with content and extra "metadata" field (ie: string=true)
Author: Kotlin
🌐
Android Developers
developer.android.com › api reference › jsonobject
JSONObject | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Stefanhendriks
stefanhendriks.com › 2021 › 01 › 29 › deserialise-json-to-typed-map-with-jackson-in-kotlin
Deserialise Json to typed Map with Jackson in Kotlin – Stefan Hendriks' blog
January 29, 2021 - val map = if (StringUtils.isEmpty(json)) { HashMap<MyEnum, Int>() } else { val typeRef: TypeReference<Map<MyEnum, Int>> = object : TypeReference<Map<MyEnum, Int>>() {} objectMapper.readValue(json, typeRef) } This means your code gets bigger (due the inline) but you do have a single function now! ... This site uses Akismet to reduce spam.
🌐
TopDealsNet
studentprojectcode.com › blog › how-to-convert-a-hashmap-to-json-in-kotlin
How to Convert A Hashmap to JSON In Kotlin in 2026?
September 20, 2025 - In Kotlin, the @SerializedName annotation is used to specify the JSON field name for a property during serialization and deserialization processes. It is typically used with libraries like Gson or Moshi to map JSON keys to Kotlin property names.
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-properties › kotlinx.serialization.properties › encode-to-string-map.html
encodeToStringMap | kotlinx.serialization – Kotlin Programming Language
kotlinx-serialization-properties/kotlinx.serialization.properties/encodeToStringMap · @ExperimentalSerializationApiinline fun <T> Properties.encodeToStringMap(value: T): Map<String, String>(source) Encodes properties from given value to a map using serializer for reified type T and returns this map.