format · validate · convert · runs locally

Parse JSON in Kotlin

How to parse a JSON string into an object and serialize (stringify) an object back to JSON in Kotlin. kotlinx.serialization is the official Kotlin JSON library; Moshi and Gson are common on Android. Need to clean up the JSON first? Use the formatter & validator.

Parse JSON (string → object)

// kotlinx.serialization
import kotlinx.serialization.json.Json
val obj = Json.parseToJsonElement(text)

Stringify (object → JSON string)

val text = Json { prettyPrint = true }
    .encodeToString(obj)

Frequently asked questions

How do I parse JSON in Kotlin?

kotlinx.serialization is the official Kotlin JSON library; Moshi and Gson are common on Android. Parse with the snippet above; the result is a native object/map you can read.

How do I pretty-print JSON in Kotlin?

Use the stringify snippet above with indentation, or paste your JSON into the formatter on this page.