Output will appear here
Output will appear here
Paste JSON and this tool writes Kotlin data class models annotated with @Serializable for kotlinx.serialization. snake_case keys become camelCase properties with @SerialName. A null value makes the type nullable (String?). A key missing from some array elements also gets = null, so decoding works when it is absent. Fields with mixed types become JsonElement.
By Ankit Aglawe · Updated · Runs entirely in your browser
poster_path is null in one result, so it is String?, but it has no default because the key is always present. The element class is named Result, which shadows kotlin.Result in that file; rename it to Movie if you also use the standard Result.
{
"page": 1,
"total_results": 2,
"results": [
{ "id": 550, "title": "Fight Club", "vote_average": 8.4, "release_date": "1999-10-15", "poster_path": "/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg", "adult": false },
{ "id": 680, "title": "Pulp Fiction", "vote_average": 8.5, "release_date": "1994-09-10", "poster_path": null, "adult": false }
]
}import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
@Serializable
data class Result(
val id: Int,
val title: String,
@SerialName("vote_average")
val voteAverage: Double,
@SerialName("release_date")
val releaseDate: String,
@SerialName("poster_path")
val posterPath: String?,
val adult: Boolean
)
@Serializable
data class Root(
val page: Int,
@SerialName("total_results")
val totalResults: Int,
val results: List<Result>
)object is a Kotlin keyword, so the property name is wrapped in backticks and needs no @SerialName. The nested class is named after its key too, giving data class Object; rename it to Invoice. customer_email is only ever null, so it becomes JsonElement?.
{
"id": "evt_1PqR2sLkdIwHu7ix",
"object": "event",
"type": "invoice.paid",
"created": 1718900422,
"livemode": false,
"data": {
"object": { "id": "in_1PqR2r", "object": "invoice", "amount_paid": 4900, "currency": "eur", "customer_email": null }
}
}import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Object(
val id: String,
val `object`: String,
@SerialName("amount_paid")
val amountPaid: Int,
val currency: String,
@SerialName("customer_email")
val customerEmail: JsonElement?
)
@Serializable
data class Data(
val `object`: Object
)
@Serializable
data class Root(
val id: String,
val `object`: String,
val type: String,
val created: Int,
val livemode: Boolean,
val data: Data
)Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for Kotlin, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Item(
val sku: String,
val qty: Int,
val note: String? = null
)
@Serializable
data class Root(
val id: Long,
val price: Double,
val ratio: Int,
@SerialName("deleted_at")
val deletedAt: JsonElement?,
@SerialName("created_at")
val createdAt: String,
val `class`: String,
val tags: List<JsonElement>,
val matrix: List<List<Double>>,
val items: List<Item>
)A key that is present with value null becomes T? with no default, so kotlinx still requires the key. A key missing from some objects becomes T? = null, which lets kotlinx decode it when absent. In the edge-case output, note gets = null and deletedAt does not.
There is no type to infer, so deleted_at becomes JsonElement?. kotlinx cannot serialize Any, and JsonElement holds any JSON value. Replace it with the real type when you have a filled-in sample.
A whole-number field is Int while every sampled value fits in 32 bits and Long once one does not, so the edge-case id comes out Long. kotlinx fails to decode an oversized number into Int, so if small sample ids can grow past 2,147,483,647 in production, switch the field to Long. Also, 1.0 reaches the converter as 1, so ratio comes out Int; use Double if it can hold fractions.
Hard keywords like class, object, in or is are kept as the property name and wrapped in backticks, Kotlin's escape for identifiers. The serial name is the property name, so no @SerialName is added for them.
Timestamps stay String. kotlinx has no built-in date serializer. If you use kotlinx-datetime, change the type to Instant and it will parse ISO 8601 strings.
[[1, 2], [3, 4.5]] becomes List<List<Double>>, since every inner value is merged. An empty array has no element type, so tags: [] becomes List<JsonElement>.
| JSON value | Kotlin type | Notes |
|---|---|---|
| string | String | Dates stay String too |
| integer | Int or Long | Long when a value exceeds 2^31 - 1 |
| number with a fraction | Double | |
| true / false | Boolean | |
| null alongside a value | T? | No default, key required |
| key missing in some objects | T? = null | |
| only ever null | JsonElement? | |
| object | @Serializable data class Name | Named after its key, PascalCase |
| array | List<T> | All elements merged |
| mixed types or empty array | JsonElement / List<JsonElement> |
Enter any JSON object or array in the input editor. The converter handles nested structures, arrays, and null values.
The output pane displays Kotlin data classes with val properties, @SerialName annotations, and proper Kotlin types including Int, Double, String, and List.
Copy or download the .kt file. The data classes are ready for use with kotlinx.serialization or Gson in your Android, Ktor, or Spring Boot project.
Paste the JSON on the left and copy the classes on the right. Apply the kotlin("plugin.serialization") Gradle plugin, add kotlinx-serialization-json, and decode with Json.decodeFromString<Root>(text).
The output targets kotlinx.serialization. Gson and Moshi ignore @SerialName, so snake_case keys will not map; you would need @SerializedName or @Json(name = ...) instead. Gson also skips Kotlin constructors, so non-null properties can end up null.
Yes, when the sample shows it: a field becomes Long if any value in it is above 2,147,483,647, and Int otherwise. The decision is made from your sample, so widen ids by hand if they can grow.
kotlinx throws on unknown keys by default. Create the decoder with Json { ignoreUnknownKeys = true }.
Yes. Add the converter-kotlinx-serialization artifact (official since Retrofit 2.10; on older versions use Jake Wharton's converter) and return the generated type from your API interface.
Only keys missing from some of the sample objects get a default. kotlinx treats a property without a default as required, so = null is what lets an absent key decode.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.