Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Dec 7, 2023
Contribute to Docs

The .decodeFromString() function is provided by the Kotlin Serialization library, used for deserializing data from a string representation (in JSON format) into a Kotlin object.


fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T
  • deserializer: An instance of DeserializationStrategy that defines how the string should be deserialized into data.
  • string: The string representation (e.g. JSON) of the data to be deserialized.


Here is an example of how to use .decodeFromString() to deserialize data from a JSON string into a Kotlin object:

import kotlinx.serialization.*
import kotlinx.serialization.json.Json
data class Person(val name: String, val age: Int)
fun main() {
val jsonString = """
"name": "Johnny",
"age": 30
// Deserialize the JSON string into a Person object using decodeFromString()
val person = Json.decodeFromString<Person>(jsonString)

The above example will result in the following output:

Person(name=Johnny, age=30)

All contributors

Looking to contribute?

Learn Kotlin on Codecademy