Skip to content
XUtilHub

JSON to Kotlin Data Class Generator

Convert JSON to Kotlin data classes for kotlinx.serialization, Gson or Moshi. camelCase properties with @SerialName, nullable types and default values.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Kotlin

Generate Kotlin data classes from JSON for kotlinx.serialization, Gson or Moshi, with the right annotations for renamed keys and nullable defaults for optional fields.

Key features

  • Kotlin data classes from any JSON sample
  • kotlinx.serialization, Gson or Moshi annotations
  • camelCase properties with @SerialName / @SerializedName / @Json
  • Nullable types with = null defaults
  • Int vs Long chosen from your values
  • Kotlin keywords escaped with backticks

Kotlin data classes from JSON

Android apps, Ktor servers and Kotlin Multiplatform projects all parse JSON into data classes. Generate them from a real response, with the annotations your serialization library expects.

Example

{ "user_id": 7, "name": "Ada", "nickname": null, "roles": ["admin"] }
@Serializable
data class Root(
    @SerialName("user_id") val userId: Int,
    val name: String,
    val nickname: JsonElement? = null,
    val roles: List<String>,
)
val root = Json.decodeFromString<Root>(json)

Library annotations

LibraryClass annotationRenamed key
kotlinx.serialization@Serializable@SerialName("user_id")
Moshi (codegen)@JsonClass(generateAdapter = true)@Json(name = "user_id")
Gson—@SerializedName("user_id")

Tips

  • kotlinx.serialization: apply the plugin.serialization Gradle plugin; use Json { ignoreUnknownKeys = true } for evolving APIs.
  • Retrofit: pair Moshi or kotlinx with the matching converter factory.
  • Dates: replace strings with Instant plus a custom serializer.

Building the iOS app too? Use JSON to Swift. For Java code, see JSON to Java.

How to use the JSON to Kotlin

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Kotlin appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Which library should I choose?

kotlinx.serialization is the modern, multiplatform choice and works well with Ktor. Moshi and Gson are common in existing Android projects using Retrofit.

Why are nullable properties defaulted to null?

So a missing key doesn't fail deserialization. kotlinx.serialization and Moshi use the default; Gson bypasses constructors, so missing values stay null anyway.

What happens with an empty object?

Data classes need at least one property, so an empty JSON object becomes a regular class.

What is JsonElement?

kotlinx.serialization's type for arbitrary JSON, used when a value's type couldn't be inferred. With Gson or no library, Any? is used.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools