JSON to Kotlin Data Class Generator
Last updated:
Jsonic's JSON to Kotlin Data Class generator converts a JSON object into Kotlin data class definitions. Each property gets a @JsonProperty annotation from the Jackson library to preserve the original JSON key name. JSON strings map to String, integers to Long, decimals to Double, booleans to Boolean. Null values produce nullable Kotlin types (String?, Long?) with a default value of null. JSON arrays become List<T>. Nested objects generate separate named data classes before the root class. Field names are converted from snake_case to camelCase following Kotlin conventions. All processing runs in your browser with no data upload.
How to convert JSON to a Kotlin data class
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally change the root class name (default: Root).
- Click Generate.
- Add Jackson to your project: implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.x.x").
- Deserialize with: val obj = objectMapper.readValue<Root>(jsonString).
FAQ
Which JSON library does the generated code use?
The generated code uses Jackson with the @JsonProperty annotation from com.fasterxml.jackson.annotation. For Android projects with Gson, replace @JsonProperty("key") with @SerializedName("key") from com.google.gson.annotations. For kotlinx.serialization, replace it with @SerialName("key") and add @Serializable to the class.
How are JSON types mapped to Kotlin types?
JSON strings → String, JSON integers → Long, JSON decimals → Double, JSON booleans → Boolean, JSON null → nullable type (String?, Long?), JSON arrays → List<T>, JSON objects → named data class. Long is used for integers to handle large numbers safely; change to Int manually if your values are guaranteed to fit in 32 bits.
Why does the generator use data class?
data class in Kotlin automatically generates equals(), hashCode(), toString(), and copy() methods based on the constructor properties. This is the standard pattern for JSON DTOs in Kotlin — it makes comparing responses and creating test fixtures straightforward.
How are snake_case JSON keys converted to Kotlin field names?
The generator converts snake_case keys (user_id) and hyphenated keys to camelCase Kotlin field names (userId) following Kotlin conventions. The original JSON key is preserved in the @JsonProperty annotation so Jackson maps the fields correctly during deserialization.
How are nullable fields handled?
Fields with null values become nullable Kotlin types (e.g., String?) with a default parameter value of null. This means the parameter is optional in the constructor — if the JSON field is missing, Jackson sets it to null automatically.
Can I use this with Android development?
Yes. For Android with Gson (common in older projects), replace @JsonProperty with @SerializedName from Gson. For Retrofit2, the generated data classes work directly as response models — Gson or Jackson deserializes the API response into the class automatically.
Can I use this with Spring Boot?
Yes. Spring Boot uses Jackson by default. The generated data classes with @JsonProperty annotations work as @RequestBody parameters and return types in Spring MVC controllers. Enable the Kotlin Jackson module in your Spring Boot app for correct null handling.
How are nested objects handled?
Each nested JSON object becomes a separate Kotlin data class. Nested classes are declared before the root class in the output file. The root class references nested classes by type name (e.g., val address: Address).