JSON to Kotlin: Data Classes for Gson
Paste a real API response and get Kotlin data classes whose types match what is actually in it, nullable fields and long IDs included.
Paste or upload your JSON
Start with JSON your Android app or Kotlin backend really receives. A field that never appears in your sample can't appear in the classes, so a fuller sample gives better types.
Example input
{
"userId": 12345,
"username": "kotlindev",
"email": "[email protected]",
"isActive": true,
"preferences": ["dark_mode", "notifications", "auto_sync"],
"profile": {
"first_name": "Android",
"last_name": "Developer",
"age": 28,
"country": "USA"
}
}Pick the class name and style
Three settings sit above the editors. The rest follows from your data, so a quick look at the types tells you whether the sample was representative.
Read the generated classes
Here is the full output for the example above. Only the snake_case keys in Profile needed @SerializedName; the camelCase ones already match.
Output for the example above
// Needs Gson (com.google.code.gson:gson). Read it with:
// val data = Gson().fromJson(json, Root::class.java)
import com.google.gson.annotations.SerializedName
data class Root(
val userId: Int,
val username: String,
val email: String,
val isActive: Boolean,
val preferences: List<String>,
val profile: Profile
)
data class Profile(
@SerializedName("first_name")
val firstName: String,
@SerializedName("last_name")
val lastName: String,
val age: Int,
val country: String
)Copy or download the .kt file
Add a package line if your project uses packages, put the file next to the code that calls your API, and read the response with Gson.
What is JSON to Kotlin Conversion?
Reading JSON in Kotlin usually means mapping it onto data classes and letting a library such as Gson fill in the properties. Writing those classes by hand is slow, and the details are easy to get wrong: which fields can be null, whether an ID fits in an Int, and how to keep keys like order_id when the property is called orderId.
This page writes them from a real sample. If your data is XML rather than JSON, the XML to Kotlin page does the same job; to tidy the JSON first, try the JSON Formatter.
Frequently Asked Questions
Which library does the generated code need?
With "Use Gson Annotations" ticked, only Gson (com.google.code.gson:gson). The classes are plain Kotlin; Gson is needed for @SerializedName, for JsonElement when a field holds mixed values, and for reading the JSON. The comment at the top of the file shows the exact Gson().fromJson call, including the TypeToken version for a JSON array. Retrofit's Gson converter reads the classes the same way.
Can I use kotlinx.serialization or Moshi instead?
The page writes Gson annotations only, but switching is a small edit. For kotlinx.serialization, add @Serializable above each class and replace @SerializedName with @SerialName; a JsonElement field then needs kotlinx.serialization.json.JsonElement. For Moshi, replace @SerializedName("key") with @Json(name = "key") and register the Kotlin adapter. Untick "Use Gson Annotations" if you would rather start from clean classes.
How does it choose between Int, Long and Double?
It looks at how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become Int, bigger ones become Long (an ID like 8901240544102066246 keeps every digit), and anything past 64 bits becomes java.math.BigInteger. A number written with a decimal point or exponent, such as 14.50 or 1e3, becomes Double, or BigDecimal if the same field also holds whole numbers too long for a Double. Text in quotes, like "00501", stays a String so the leading zeros survive.
Why are some properties nullable with = null?
A property becomes nullable when its value is null somewhere in your JSON or when it is missing from some items of a list, and it gets = null as its default. Everything else stays non-null. Keep in mind that Gson does not know about Kotlin nullability or default values: if a later response leaves out a non-null field, Gson still sets it to null, so paste a sample that shows every field you expect.
How are odd JSON keys turned into Kotlin names?
Keys are split into words and joined in camelCase, so "order_id" becomes orderId. Kotlin keywords get Value added (class becomes classValue), keys that start with a digit get a field prefix, and names that collide, like "first-name" and "first_name", get a number. Class names that would hide String, List or Any get Model added. With Gson annotations on, @SerializedName keeps the original key, and a $ in a key is escaped so it never starts a string template.
Is my JSON sent anywhere?
No. The classes are generated in your browser as you type, and nothing you paste or upload is sent to our server or kept after you leave the page.