N
NJHTools

JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for every nested object, with nullable properties where your sample shows missing or null values. Free, no signup, and generated in your browser.

Language

16 lines · processed in your browser, nothing is uploaded

Kotlin output · Root.kt

Generated code appears here.

Example: JSON to Kotlin

This JSON sample, with the root type named User, produces the Kotlin below using the default options.

Input JSON
{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "active": true,
  "score": 98.5,
  "tags": [
    "admin",
    "beta"
  ],
  "address": {
    "city": "London",
    "postcode": "NW1 6XE"
  }
}
Generated Kotlin
// To parse the JSON, install kotlin's serialization plugin and do:
//
// val json = Json { allowStructuredMapKeys = true }
// val user = json.parse(User.serializer(), jsonString)

package com.example

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*

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

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

How to convert JSON to Kotlin

  1. 1Paste a JSON object or array into the input.
  2. 2Select Kotlin as the output language.
  3. 3Name the root class, for example Profile.
  4. 4Adjust options such as the serialization framework if available.
  5. 5Copy the data classes into a .kt file.

kotlinx.serialization or Moshi?

kotlinx.serialization is JetBrains' official library: annotate a class with @Serializable, apply the compiler plugin, and decode with Json.decodeFromString<Profile>(text). It works in Kotlin Multiplatform projects, not just on the JVM.

Moshi is popular on Android alongside Retrofit. Use @JsonClass(generateAdapter = true) with codegen so it understands Kotlin nullability and default values instead of relying on reflection.

Renaming keys

To keep camelCase property names for snake_case keys, use @SerialName("user_id") with kotlinx.serialization or @Json(name = "user_id") with Moshi.

kotlinx.serialization throws on unknown keys by default; create the decoder with Json { ignoreUnknownKeys = true } if the API may add fields.

Nullability and defaults

A property typed String? accepts null, but a missing key still fails unless the property has a default value. Give optional properties a default such as = null.

Paste several records so the generator can see which fields are sometimes absent.

Frequently Asked Questions

How do I convert JSON to a Kotlin data class?↓

Paste the JSON, choose Kotlin and copy the generated data classes. Each nested object becomes its own data class.

Should I use kotlinx.serialization or Moshi?↓

Choose kotlinx.serialization for Multiplatform or new projects, and Moshi if your Android stack already uses it with Retrofit. Both handle Kotlin nullability correctly when configured with codegen or the compiler plugin.

How are optional fields detected?↓

Types are inferred from the sample you paste. A field becomes optional only if it is missing from some of the objects, so paste an array with several representative records, or mark the field optional by hand after generating.

Does it handle nested arrays?↓

Yes. Arrays of objects become List<Item> with a generated Item class, and nested arrays become nested List types.

Is my JSON uploaded to a server?↓

No. The code generator (quicktype) runs entirely in your browser, so the JSON you paste never leaves your device. That makes it safe to use with internal API responses, though you should still strip secrets before sharing generated code.

Last updated: