JSON to Kotlin Data Class
Paste a sample JSON object below and generate Kotlin `data class` definitions. The tool infers a type for every field and creates a named data class for each nested object, ready for Gson or kotlinx.serialization.
Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up. Emits idiomatic data classes with @SerializedName for non-identifier keys, so Gson binding works immediately.
How to generate a Kotlin data class from JSON
- Paste your JSON. Paste one representative JSON object into the box above.
- Generate the data class. Click Generate Kotlin data class. The tool infers a type for every field and emits a data class for the root and each nested object.
- Deserialize with Gson. Copy the classes into your project and call Gson().fromJson(json, Root::class.java).
How the type mapping works
Each JSON value maps to a Kotlin type: a whole number becomes Long, a decimal becomes Double, a string becomes String, true/false becomes Boolean, and null becomes Any?. Arrays become List<T>, and keys that are not valid Kotlin identifiers get a Gson @SerializedName annotation so the property name stays idiomatic.
Arrays and nested objects
An array becomes a typed List using the type of its first element, and every nested object gets its own named data class so the result compiles as-is. For fields that may be missing in some responses, mark them nullable with ? and give a default.
Using the result
With Gson: val root = Gson().fromJson(json, Root::class.java). With kotlinx.serialization, add @Serializable and use Json.decodeFromString.
FAQ
Does this use Gson or kotlinx.serialization?
The generated classes are plain Kotlin data classes with Gson @SerializedName for renamed keys. They also work with kotlinx.serialization if you add @Serializable and @SerialName instead.
How do I handle optional or nullable fields?
A single sample cannot show which fields are absent elsewhere. Mark fields that may be missing as nullable (Type?) and optionally give a default value so deserialization never fails.
Why Long and Double?
JSON has one number type. The tool picks Long for whole numbers and Double for decimals. Narrow to Int or Float where the real range is known.