JSON1 / JSON to Kotlin
JSON to Kotlin
Generate Kotlin data classes with kotlinx.serialization from a JSON sample. A SerialName annotation is added only where the JSON key differs from the property name.
Processed locally·Your data never leaves your device
InputJSON
ReadyOutputKotlin
Converted output appears here as you type.
Good to know
- Every record in an array is inspected, not just the first, so a field that appears in only some of them is typed as optional rather than required.
- Identical nested shapes are emitted once and reused, and reserved words are renamed to something legal in this language.
About this tool
A Kotlin data class gives you equality, a copy method, and destructuring for free, which makes it the natural target for a JSON payload. This generates one per object with kotlinx.serialization annotations.
Keys become camelCase properties, and nested objects are hoisted into their own classes rather than inlined.
- When is a SerialName annotation added?
- Only when the JSON key differs from the property name after camelCasing. Adding it everywhere would be noise, so simple models stay clean.
- Why kotlinx.serialization and not Moshi or Gson?
- kotlinx.serialization is the JetBrains-maintained option and understands Kotlin's nullability and default values without reflection, which matters because a data class with defaults is exactly what Gson tends to mishandle. Swapping to Moshi means changing the annotations, not the class.
- How are optional fields decided?
- Every record in an array is inspected, not just the first. A field that appears in some records and not others is typed as optional, and a value that is sometimes null widens accordingly. Generators that read only the first element get this wrong on exactly the payloads where it matters.
- What happens to a key that is a reserved word?
- It is renamed to something legal in the target language, and where the language supports it, an annotation records the original JSON key so serialisation still round-trips.