JSON to Kotlin
Paste a JSON sample. Get Kotlin types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Kotlin chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Kotlin:
package quicktype
import java.time.OffsetDateTime
data class Root (
val id: Long,
val name: String,
val active: Boolean,
val joined: OffsetDateTime,
val tags: List<String>,
val address: Address,
val scores: List<Double>
)
data class Address (
val city: String,
val postcode: Any? = null
)
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// To parse the JSON, install jackson-module-kotlin and do:
//
// val root = Root.fromJson(jsonString)
package quicktype
import java.time.OffsetDateTime
import com.fasterxml.jackson.annotation.*
import com.fasterxml.jackson.core.*
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.node.*
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.module.kotlin.*
@Suppress("UNCHECKED_CAST")
private fun <T> ObjectMapper.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonNode) -> T, toJson: (T) -> String, isUnion: Boolean = false) = registerModule(SimpleModule().apply {
addSerializer(k.java as Class<T>, object : StdSerializer<T>(k.java as Class<T>) {
override fun serialize(value: T, gen: JsonGenerator, provider: SerializerProvider) = gen.writeRawValue(toJson(value))
})
addDeserializer(k.java as Class<T>, object : StdDeserializer<T>(k.java as Class<T>) {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = fromJson(p.readValueAsTree())
})
})
val mapper = jacksonObjectMapper().apply {
propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE
setSerializationInclusion(JsonInclude.Include.NON_NULL)
convert(OffsetDateTime::class, { OffsetDateTime.parse(it.asText()) }, { "\"${java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(it)}\"" })
}
data class Root (
@get:JsonProperty(required=true)@field:JsonProperty(required=true)
val id: Long,
@get:JsonProperty(required=true)@field:JsonProperty(required=true)
val name: String,
@get:JsonProperty(required=true)@field:JsonProperty(required=true)
val active: Boolean,
@get:JsonProperty(required=true)@field:JsonProperty(required=true)
and 25 more lines. Open the tool to see the whole file.
What you need in your project
Jackson with the Kotlin module, or kotlinx.serialization.
The default output uses Jackson with `jackson-module-kotlin`, which understands a data class and its default values.
A data class gives you equality, a copy method and a readable `toString` with no extra lines.
The trap to know about
A non-null Kotlin property with a missing key throws at read time, not at build time. The Kotlin module is what makes that message clear, so do not leave it out.
The options you can change
These belong to Kotlin, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Framework | Serialization framework | jackson | klaxon | kotlinx |
| Package name | Package | quicktype |
Questions
How do I turn JSON into Kotlin?
Paste your JSON into the box, choose Kotlin, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Kotlin project?
Jackson with the Kotlin module, or kotlinx.serialization.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code