JSON to Kotlin Data Class Converter
FV-17Paste a JSON sample and press Convert to get Kotlin data classes, with kotlinx.serialization annotations if you want them. Nullable and optional keys are typed for you. It runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
Kotlin
How to use it
- Paste a JSON sample into the input box, or open a .json file.
- Leave the kotlinx.serialization option on for @Serializable and @SerialName, or turn it off for plain data classes.
- Press Convert and copy the classes into your project.
- Decode with Json.decodeFromString<Root>(text) when serialization is on.
What the data classes look like
Each object becomes a data class with val properties in camelCase, and nested objects become their own classes. With the serialization option on, every class carries @Serializable and a property whose name differs from its key gets @SerialName with the original key. Arrays become List<T>, with the item type worked out from every item. Whole numbers become Int, or Long beyond the 32-bit range, and decimals become Double.
Null, missing and mixed values
A key that is null in some items is typed T?. A key missing from some objects is typed T? = null so decoding still works without it. Mixed types, keys that are always null and empty arrays have no single type. With serialization on they become JsonElement, because kotlinx.serialization has no serializer for Any. With it off they become Any.
Limits to know about
A whole number beyond Long becomes Double and can lose digits, and the line gets a comment. An object with no keys becomes a plain class, since a Kotlin data class needs at least one property. The tool only sees your sample, so a key that never appears has no property, and dates stay String. Check the result against the API documentation.
Questions people ask
How do I convert JSON to a Kotlin data class?
Paste the JSON, press Convert, and copy the classes. With the serialization option on, add the kotlinx.serialization plugin and library, then call Json.decodeFromString<Root>(text). The class named Root matches the top-level object.
What does @SerialName do here?
It tells kotlinx.serialization which JSON key a property reads. It is added only when the Kotlin property name, written in camelCase, is not the same as the key, such as first-name or user_id.
Why does a mixed field become JsonElement?
kotlinx.serialization cannot serialize Any, so a field that holds different types needs JsonElement, which can hold any JSON value. Turn the serialization option off if you use another library, and the field becomes Any.
What is the difference between T? and T? = null?
T? means the key is present but can be null in your sample. T? = null means the key is missing from some objects, so the default lets decoding succeed when it is absent.
Can I use it with Moshi or Gson?
The classes are plain data classes, so they work with other libraries if you turn the serialization option off. The tool does not write annotations for Moshi or Gson, so add them yourself where a key name differs.
Why is my big number a Double?
A whole number beyond Long has no exact simple type in the output, so it becomes Double, which can round it. A comment marks the property. Change it to a String or a BigInteger with a custom serializer if you need every digit.
About this tool
Infers types from the sample you paste, so it only knows what that sample shows. Each object becomes a data class with val properties in camelCase. With the serialization option on, every class gets @Serializable and a property whose name differs from its key gets @SerialName with the original key. A key that is null in some items is typed T? and a key missing from some objects is T? = null. Whole numbers become Int, or Long beyond the 32-bit range, and anything with a decimal point or exponent becomes Double; a whole number beyond Long also becomes Double and can lose digits. Arrays become List<T>. Mixed types, null-only keys and empty arrays become JsonElement with the serialization option on, because kotlinx.serialization has no serializer for Any, and Any with it off. An object with no keys becomes a plain class, since a data class needs at least one property.