JSON to Kotlin Data Class Converter

Paste a JSON sample and get Kotlin data classes annotated for kotlinx.serialization: one class per object, typed properties, List for arrays and @SerialName where a key isn't a valid property name.

Input Code
Ready - Type or paste code in the left panel
Output Code
Ready - Type or paste code in the left panel

How to convert JSON to Kotlin

  1. Paste your JSON into the left panel, or click Load Sample.
  2. Kotlin is already selected as the output on the right.
  3. Click Convert, then Copy Output.

Example

This JSON:

{ "id": 101, "name": "Ada Lovelace", "email": "ada@example.com", "active": true, "balance": 250.75,
  "address": { "city": "London", "postcode": "N1 9GU" }, "tags": ["admin", "beta"] }

becomes:

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Root(
    val id: Long,
    val name: String,
    val email: String,
    val active: Boolean,
    val balance: Double,
    val address: Address,
    val tags: List<String>
)

@Serializable
data class Address(
    val city: String,
    val postcode: String
)

How JSON types map to Kotlin

JSON valueKotlin type
"text"String
101Long
250.75Double
trueBoolean
{ … }a new data class named after the key
["a", "b"]List<String>

Decoding JSON with kotlinx.serialization

val json = Json { ignoreUnknownKeys = true }
val root = json.decodeFromString<Root>(text)
println(root.address.city)

The classes need the kotlinx.serialization Gradle plugin and runtime library. ignoreUnknownKeys keeps decoding working when the API adds new fields.

Using Gson or Moshi instead

The data classes work with other libraries too: remove the @Serializable annotations, and replace @SerialName with Gson's @SerializedName or Moshi's @Json(name = …).

Optional fields

Paste an array of objects to let the generator see which fields are sometimes missing or null; those become nullable types with a null default.

Is my JSON sent anywhere?

The code is generated in your browser; your JSON never leaves your computer. See the privacy policy.

Frequently asked questions

How do I convert JSON to a Kotlin data class?

Paste the JSON on the left, keep Kotlin selected on the right and click Convert. Copy the data classes into your project.

Which library are the classes for?

kotlinx.serialization. They also work with Gson or Moshi after swapping the annotations.

What does @SerialName do?

It maps a JSON key that isn't a valid Kotlin property name, such as first-name, to a camelCase property.

Is my JSON uploaded?

No. Kotlin data classes are generated in your browser.

Other languages: JSON to Java · JSON to TypeScript · JSON to Go · JSON to Python · JSON to C# · all languages

Share Content

Choose what to share and generate an instant link

🔥 One-time link Content deleted from server after first view
✉️
Uploading to server…