JSON to Kotlin Converter
Kotlin data classes from a JSON sample, with optional kotlinx.serialization annotations
Input
Output
Paste your JSON on the left and the Kotlin code shows up here.
Or drop a file on this box, , or .
What does the JSON to Kotlin converter generate?
A help-desk API hands back tickets with nested attachments, and Kotlin wants a data class for each level before Json.decodeFromString<RootObject>(text) will touch them. Paste one ticket and those classes appear: val properties in camelCase, your package line, and nested classes declared ahead of the ones that use them. With kotlinx.serialization ticked, the default, every class carries @Serializable and a key such as user-id becomes @SerialName("user-id") val userId, ready for the kotlinx.serialization compiler plugin.
Every value in the sample counts. "openedAtMs": 1730620800000 does not fit in 32 bits, so it becomes Long, and positive values beyond the signed 64-bit range get ULong. Arrays map to List<T> and arrays of arrays to List<List<T>>. A value whose type changes between records, or that was null every time, is typed JsonElement? in serialization mode and Any? without it, while an empty {} becomes JsonObject or Map<String, Any?>. Keys that collide with Kotlin's hard keywords, such as class or in, keep their names inside backticks.
With Nullable Types on, a property the sample showed as null or left out of some items, like thumbnail on the first attachment, is declared Boolean? = null, so decoding still succeeds when the key is absent (the null safety guide covers why that default matters). Turn serialization off and you get bare data classes with a // JSON key: "user-id" comment beside each renamed property; Gson's @SerializedName or Moshi's @Json(name = ...) are yours to add. Nothing here calls a server.
How to use it
The classes update as you type.
Paste a JSON sample
The Input panel takes a paste or a file through Upload, and Sample loads a nested telecom subscriber. A help-desk ticket shows a renamed key, a 64-bit timestamp and an optional field together:
{"ticketId":"TCK-88213","subject":"Router keeps rebooting","priority":2,"openedAtMs":1730620800000,"assignee":null,"user-id":"u_4471","attachments":[{"name":"syslog.txt","bytes":20480},{"name":"wan.png","bytes":88214,"thumbnail":true}]} Class Name sets the root class and the download's file name (RootObject.kt); Package sets the first line. A stray comma shows up as Invalid JSON: plus the parser's message where the code would be, and the JSON Validator pins that to a line.
Pick the options
Flip kotlinx.serialization and Nullable Types and the Output panel redraws. For the ticket above, serialization mode gives an Attachment class with val thumbnail: Boolean? = null and a root whose assignee is JsonElement? = null; with both options off they read Boolean and Any?.
Copy or download
Copy the code or Download a .kt file named after the root class.
Where this saves time
Android API models
Paste a response from your backend and get models for Retrofit with the kotlinx.serialization converter, including nullable defaults for fields the server sometimes leaves out.
Ktor clients and servers
Ktor's ContentNegotiation plugin works with @Serializable classes, so generated request and response types drop straight into routes and HttpClient calls.
Kotlin Multiplatform
kotlinx.serialization runs on JVM, Android, iOS and JS, so the same generated models can live in a shared module.
Finding optional fields
Paste several real records as an array. Anything absent or null in one of them becomes nullable with a = null default, which shows where your code needs a fallback.
Common Questions
Which serialization library does it target?
With the option ticked, kotlinx.serialization: @Serializable on classes and @SerialName on renamed properties. With it off you get plain data classes that Gson, Moshi or Jackson can read, but you add their rename annotations yourself.
What does Nullable Types change?
On: a property seen as null, or missing from some array items, gets a ? and = null, as thumbnail does on the attachments above. Off: those properties keep the plain type, which is stricter and makes kotlinx.serialization throw MissingFieldException when the key really is absent. A key that was null every time stays nullable in both modes.
How are large integers handled?
Digits are kept exactly while the page reads your JSON, so 8901240544102066246 becomes Long and not a rounded double. A positive value up to 18446744073709551615 gets ULong; one past that, like 18446744073709551616, is typed JsonElement, or BigInteger (with a java.math import) when serialization is off.
Has the output been compiled?
We check it against Kotlin's keyword rules and type syntax, but there's no Kotlin compiler behind this page, so give it a build in your project and review null-only fields, which come out as JsonElement? or Any?.
What if my root is an array?
The objects inside supply the class named in Class Name, with a comment above it pointing you at List<Ticket> as the decode target. A root that is a lone string or something like [3, 5, 8] has nothing to model, so you get a one-line explanation where the code would be.
Is my JSON uploaded?
No. The Kotlin is written by a generator compiled into this page, and typing never sends your ticket data out. Inputs over 256 KB are refused, so cut a long array down to a handful of entries before pasting.
Other JSON Tools You Might Need
Need the same models in Java or Swift, or want to validate the JSON first?