JSON to Kotlin
Generate Kotlin data classes from JSON for kotlinx.serialization, Gson, Moshi or Jackson
Paste JSON or load an example to generate Kotlin code.
What JSON to Kotlin generates
JSON to Kotlin turns an API response or any JSON sample into Kotlin data classes for kotlinx.serialization, Gson, Moshi, Jackson or no library at all. When a property name differs from its key, for example user_name → userName, the chosen library annotation (@SerialName, @SerializedName, @Json or @JsonProperty) maps it back to the JSON. Hard keywords such as class, object, when or fun are written in backticks, and keys that are not valid names, such as 123abc or user-name, are renamed with a mapping or kept in backticks, depending on the naming setting.
How to use
- 1.Paste JSON, upload or drop a .json file, or load one of the examples.
- 2.Set the root type name, the property naming and the nullability.
- 3.Choose the serialization library, and turn on default values if you want every class to have a no-argument constructor.
- 4.Under Advanced, set a package or nest the classes inside the root class.
- 5.Read the notes under the output, then copy the code or download the .kt file.
How types are inferred
- •Every array item is inspected, not just the first one, and objects in the same array are merged.
- •A key missing from some objects is optional and becomes T? = null; a key that is sometimes null becomes T?. The two are kept apart, so {"id": 1, "name": "A"} and {"id": 2} give val name: String? = null.
- •Integers that fit 32 bits become Int, larger ones Long, and values beyond 64 bits BigInteger (JsonPrimitive with kotlinx.serialization). Values beyond ±2^53 get a note, because JavaScript clients may round them.
- •1 and 2.5 in the same position widen to Double; other mixes are reported.
- •Type mapping: string → String, decimal → Double, boolean → Boolean, array → List<T>, object → data class, an empty object → class, unknown or mixed → Any? (JsonElement? with kotlinx.serialization).
Limits and privacy
A sample shows what the data looked like once, not the full API contract. Review optional fields, generic types and number widths before relying on the code. The generated code needs the usual library setup: the serialization compiler plugin, Moshi code generation or jackson-module-kotlin. Gson bypasses Kotlin constructors, so non-null properties can still receive null unless every property has a default. Input is limited to 5 MB and 256 nesting levels; from 2 MB, generation waits for the Generate button. Everything runs in your browser and the JSON is never uploaded.
