JSON to Kotlin Data Class
Paste JSON and get Kotlin data classes for kotlinx.serialization, Moshi or Gson. val or var, nullable fields handled. Private.
Paste JSON on the left. Add a second sample to find optional fields.
JSON input
Stays in your browserKotlin code
Shortcuts: Ctrl+Enter run · Ctrl+Shift+C copy result · / search (use ⌘ on Mac)
Next steps
Need the same types in another language? JSON to TypeScript, JSON to Go Struct, JSON to Rust Struct, JSON to C# Class, JSON to Java POJO, JSON to Python Dataclass. Or validate a document against a JSON Schema.
Free Online JSON to Kotlin Data Class Generator
Create Kotlin data classes from a JSON sample for the serialization library you use. It gives developers a quick browser-based way to solve a focused json to kotlin data class task before moving the result into a project.
How to generate Kotlin code from JSON
Paste a JSON object or an array of objects into the input box and press Generate. Pick kotlinx.serialization, Moshi or Gson and the right annotations and imports are written for you. Optional and nullable fields default to null.
One input, every language
Your JSON stays in the box when you switch the language tabs, and the code is generated again at once. Each language also has its own page, so you can bookmark the one you use.
How types are worked out
- Whole numbers and numbers with decimals are told apart.
- Add more samples with Add sample: a key missing from some samples becomes optional, and null makes a type nullable.
- Arrays of objects are merged into one type, and nested objects become their own types named after the key.
- Keys that are not valid names in the language (such as first-name or 2fa) are renamed and mapped back, never rejected.
Worked example
The sample JSON in the box above, generated with the default options:
{
"id": 1,
"name": "Ada",
"email": null,
"tags": ["x"],
"address": { "city": "Pune", "pin": "411001" },
"createdAt": "2026-01-15T10:30:00Z"
}import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Root(
@SerialName("id") val id: Long,
@SerialName("name") val name: String,
@SerialName("email") val email: String? = null,
@SerialName("tags") val tags: List<String>,
@SerialName("address") val address: Address,
@SerialName("createdAt") val createdAt: String
)
@Serializable
data class Address(
@SerialName("city") val city: String,
@SerialName("pin") val pin: String
)
Common mistakes
- A single sample cannot show which fields are optional. Paste two or three different responses for a realistic result.
- A value that is always null gives no type information, so it is treated as a nullable string. Change it by hand if it is something else.
- Numbers are typed from the samples: 1 and 1.0 look the same in JSON, so use a sample with decimals where you need a float.