Paste a JSON object or API response and get ready-to-use Kotlin data classes with camelCase properties, nullable types and @SerialName, @SerializedName, @Json or @JsonProperty annotations. Everything runs in your browser.
Last updated:
Your JSON is parsed locally in this tab and never uploaded. Tip: Ctrl + Enter generates.
Advertisement
Advertisement
How to use the JSON to Kotlin Data Class
Paste your JSON into the left box, or open a .json file. A sample is loaded so you can see the output right away.
Name the root class and set the package (leave it blank to omit the package line).
Pick your serialization library: kotlinx.serialization, Gson, Moshi, Jackson or none.
Choose nullability and style: nullable properties, = null defaults, Int or Long, val or var, nested classes.
Press Generate Kotlin Classes, then copy the code or download the .kt file into your project.
Example: JSON to Kotlin data classes
This API response has a snake_case key, a nested object, an array of objects and a value that can be null:
With kotlinx.serialization and the default options the generator produces:
@Serializable
data class Root(
@SerialName("user_id") val userId: Int,
@SerialName("display_name") val displayName: String,
val avatar: JsonElement? = null,
val posts: List<Post>
)
@Serializable
data class Post(
val id: Int,
val title: String,
val score: Double,
val cover: String? = null
)
score is Double because one item has 4.5, and cover is nullable with a default because only one item has it. avatar is only ever null, so its real type cannot be known: it becomes JsonElement? (or Any? for other libraries). Replace it with String? once you know.
How JSON values map to Kotlin types
JSON value
Kotlin type
"text"
String
42
Int, or Long outside ±2,147,483,647
3.14, 1e6
Double
true / false
Boolean
"x" or null, or a missing key
String? = null
{ … }
A data class (top-level or nested)
{}
JsonObject / Map<String, Any?>
[1, 2] / []
List<Int> / List<JsonElement>
[1, "two"] or always null
JsonElement / Any?
Name annotations by library
Library
Class annotation
Property annotation
Decode
kotlinx.serialization
@Serializable
@SerialName("key")
Json.decodeFromString<Root>(text)
Gson
none
@SerializedName("key")
Gson().fromJson(text, Root::class.java)
Moshi (codegen)
@JsonClass(generateAdapter = true)
@Json(name = "key")
moshi.adapter(Root::class.java).fromJson(text)
Jackson
none (register KotlinModule)
@JsonProperty("key")
jacksonObjectMapper().readValue<Root>(text)
Unknown keys: kotlinx.serialization throws on keys that have no property unless you create the parser with Json { ignoreUnknownKeys = true }. Gson and Moshi ignore them; Jackson fails unless FAIL_ON_UNKNOWN_PROPERTIES is disabled.
kotlinx.serialization needs its compiler plugin (plugin.serialization in Gradle); Moshi codegen needs KSP or kapt with moshi-kotlin-codegen.
Large IDs: keep Long for numeric IDs above 2,147,483,647, such as Twitter-style snowflake IDs.
Frequently asked questions
How are Kotlin property names created from JSON keys?
Each key is split into words at underscores, dashes, spaces and camelCase boundaries and joined in camelCase, so user_name becomes userName and email-notifications becomes emailNotifications. When the property name differs from the key, the generator adds the name annotation of your library (@SerialName, @SerializedName, @Json or @JsonProperty) so decoding still matches the JSON.
Which properties become nullable?
By default a property is nullable (for example String?) when its value is null somewhere in the JSON or when the key is missing from some items of an array. You can also make every property nullable, which is the safest choice for APIs you do not control, or none. With the default option on, nullable properties get = null so a missing key does not fail decoding.
Why do kotlinx.serialization classes need @Serializable?
kotlinx.serialization generates the serializer at compile time with its Gradle plugin, and it only does so for classes marked @Serializable. Values whose type cannot be known, such as a field that is always null or mixes strings and numbers, are typed as JsonElement so they can still be decoded.
Does Gson respect Kotlin default values and non-null types?
Not fully. Unless every property has a default value, Gson creates objects without calling the Kotlin constructor, so a missing key can leave a non-null property holding null and defaults are skipped. If your JSON may omit fields, choose 'All properties' nullable with Gson, or use kotlinx.serialization or Moshi codegen, which honour Kotlin nullability and defaults.
How are numbers typed?
Whole numbers become Int, or Long when any value is outside the 32-bit range (−2,147,483,648 to 2,147,483,647); you can also force Long everywhere. Numbers with a decimal point or exponent become Double. If a field mixes whole and decimal numbers, Double is used.
What happens with a JSON array at the top level?
The item type is generated as a data class and a comment reminds you to decode the JSON as List<Item>, for example Json.decodeFromString<List<Item>>(text). A top-level array of plain values becomes a typealias such as typealias Root = List<String>.
Is my JSON uploaded?
No. Parsing and code generation are plain JavaScript running in this page, so API responses with personal or secret data never leave your device.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.