JSON to Kotlin Data Class
Generate nested Kotlin data classes with nullable defaults and kotlinx or Jackson annotations
Inferred from samples, not a complete API contract. Missing fields and explicit null are inferred separately but can share an output type; null array elements remain nullable. Duplicate keys and numbers that would change value are rejected.
Limits: 1 MiB, 64 levels, 20,000 values, 200 models and 2,000 model fields. Use strings for exact large IDs or monetary decimals.
Target: Kotlin 2.1+. kotlinx mode needs the serialization compiler plugin and JSON runtime; Jackson mode needs jackson-module-kotlin. Mixed values use the matching JSON node type. Keys are limited to 16,000 UTF-16 units; constructor arguments must fit the JVM slot budget. Use Json { encodeDefaults = true } to retain explicit nulls when encoding with kotlinx; Jackson mode rejects empty keys.
After generation, compile in your target project and verify deserialization with representative responses.
About this tool
JSON to Kotlin Data Class infers Kotlin 2.1+ models for kotlinx.serialization, Jackson, or an unannotated project model. Nullable array elements receive their own question mark, independent of the field itself. In kotlinx mode, mixed values use JsonElement rather than an unserializable Any, and empty objects produce serializable ordinary classes because a data class needs at least one constructor property. Dollar signs and other special characters in SerialName strings are escaped as literal JSON keys. Jackson mode uses constructor and getter mappings, needs jackson-module-kotlin, and rejects empty keys because an empty JsonProperty annotation selects a default name. Optional null defaults allow missing fields to decode, but disable them only when missing constructor values should be rejected. With kotlinx, configure Json { encodeDefaults = true } when re-encoding must retain explicit null fields; missing and null presence cannot be recovered from a nullable model alone. Input stays local and is bounded to 1 MiB, 64 levels, 20,000 values, 200 models and 2,000 fields. Duplicate keys, malformed Unicode, unsafe integers and changed numeric values are rejected. Add the selected runtime and compiler plugin in the target project and test responses before refining date, enum or money types. JSON keys are limited to 16,000 UTF-16 code units and generated identifiers to 80 characters before suffixes. JVM argument checks include generated default masks: up to 124 non-null Long/Double fields or 245 reference fields can fit a single data class.
Scenario Recipes
Decode a nullable Kotlin list with a literal key
Goal: A model whose key remains literal and whose list accepts null elements.
- Paste {"$value":[1,null,2]} and choose kotlinx.serialization.
- Confirm SerialName escapes the dollar sign and the type is List<Long?>.
- Compile with the serialization plugin and JSON runtime, then decode the sample and verify the middle element is null.
Result: A model whose key remains literal and whose list accepts null elements.
Failure Clinic (Common Pitfalls)
A missing field throws despite a nullable type
Cause: A nullable constructor parameter without a default can still be required by the serializer.
Fix: Keep null defaults for inferred missing fields, or explicitly test and require presence in the application.
Dollar signs in a JSON key become Kotlin interpolation
Cause: Kotlin string templates interpret unescaped dollar syntax.
Fix: Use the generated escaped SerialName string and read back the original key in a serializer test.
Production Snippets
Keep a literal dollar key and nullable elements
kotlin
// Input: {"$value":[1,null,2]}
@Serializable
data class ApiResponse(
@SerialName("\$value")
val value: List<Long?>
)
// Imports: kotlinx.serialization.Serializable and SerialNameFrequently Asked Questions
Why does an empty object generate a normal class?
Kotlin data classes require a constructor property. An empty serializable ordinary class represents {} without an artificial field.
Why is JsonElement used for mixed values?
The kotlinx compiler plugin cannot infer a serializer for arbitrary Any. JsonElement can retain supported JSON values until a domain type is defined.
Why did a null field disappear when I encoded the model?
A null default can be omitted by the default kotlinx encoder. Use Json { encodeDefaults = true } to keep default-valued fields, accepting that missing fields can also appear as null.
Does Jackson mode need the Kotlin module?
Yes. Configure jackson-module-kotlin for Kotlin constructor handling. Generated annotations alone do not install or register that module.
Keep browsing