Draft Kotlin data classes from a JSON API sample
JSON to Kotlin creates a draft Kotlin model from a sample JSON response. It helps mobile or JVM developers begin mapping a nested API shape.
When should you use it?
- Sketch data classes for a Kotlin client.
- Inspect nested arrays in a response model.
- Compare sample data with an existing serializer contract.
Example: infer Kotlin fields from JSON
This abbreviated example shows the inferred name and active fields in Kotlin. The full generated file includes imports and helpers and may change with generator versions; review it before adding it to a project.
{"name":"Alex","active":true}data class Root(
val name: String,
val active: Boolean
)Match the model to your JSON library
A Kotlin data class describes fields, while the serializer determines how wire names, constructors, defaults, and unknown keys are handled. Review the imports, annotations, and helper code in the full generated output. Adapt the model deliberately if your application uses a different serialization library or naming strategy.
Use nullability to express the contract
A nullable field, a constructor default, and an omitted JSON member are related but different cases. Decide which ones the API permits before treating the inferred type as final. A sample containing only a successful response cannot show every error or variant. Test at least the absent-field and null cases for values that are optional in the contract.
How do I generate Kotlin data classes from nested JSON?
Generate Kotlin source from a realistic nested sample. The result models observed objects and arrays as Kotlin types and may include Jackson serialization support. Use it as a draft for your chosen Android or JVM serialization stack.
Illustrative Kotlin data class
kotlindata class Root (
val name: String,
val active: Boolean
)The full .kt file includes annotations and helper code. Check library compatibility, property naming, and nullability.
Can an empty array reveal its Kotlin item type?
An empty sample contains no item values to infer. Add representative items or edit the type manually to match the API contract.
What should you check before using the result?
Confirm nullability, property names, serialization annotations, and library compatibility in your project. Generated declarations do not validate future responses.
Understand JSON syntaxTechnical reference: quicktype model generation documentation