What JSON to Kotlin does
JSON to Kotlin turns a sample JSON document into Kotlin code: one data class per object, with a type for every field inferred from the value in the sample.
JSON to Kotlin examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
data class Address(
val city: String
)
data class Root(
val name: String,
val active: Boolean,
val tags: List<String>,
val address: Address
)This is the input that the Load sample button fills in.
Awkward keys, a null, a decimal and arrays
Input
{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}Output
data class Items(
val sku: String,
val qty: Int,
val gift: Boolean
)
data class Root(
val order_id: Int,
val total: Double,
val paid: Boolean,
val coupon: Any,
val items: List<Items>,
val tags: List<Any>
)The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Kotlin type:
| Sample value | Kotlin type |
|---|---|
| String | String |
| Whole number | Int |
| Decimal number | Double |
| true / false | Boolean |
| null, or an empty array’s items | Any |
| Array of T | List<T> |
| Object | a new named type |
Using the generated code
For kotlinx.serialization, add @Serializable above each class, then:
val root = Json.decodeFromString<Root>(text)Details and limits
Any, used for null values and empty arrays, cannot be serialised by kotlinx.serialization. Replace it with the real type or withJsonElement.- A renamed key needs
@SerialName("order-id")on the property. - Properties are non-null
vals. Add?and a default ofnullto the optional ones. - A key that is a Kotlin keyword, such as
class, must be renamed or written in backticks. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
Is my data uploaded when I use JSON to Kotlin?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does JSON to Kotlin do?
JSON to Kotlin turns a sample JSON document into Kotlin code: one data class per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
Any, used for null values and empty arrays, cannot be serialised by kotlinx.serialization. Replace it with the real type or with JsonElement. A renamed key needs @SerialName("order-id") on the property.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.