JSON to Kotlin Converter
Turn a JSON sample into Kotlin data classes with kotlinx.serialization annotations.
How to generate Kotlin code from JSON
Paste your JSON sample
Paste or type it into the box, or press Try an example. Use a real response that has every field you expect.
Name the main type
Set the name of the top-level type, if you want something other than Root.
Check the Kotlin output
Nested objects become their own types. Rename fields or mark optional ones to suit your API.
A field missing from your sample will be missing from the result.
Use it in your project
Paste the data classes into a .kt file.
Worked example
This is what JSON to Kotlin Converter gives for its own sample values. Open the tool above and change anything to try your own.
JSON sample
{
"id": 42,
"user-name": "ada",
"score": 9.5,
"active": true,
"tags": ["admin", "beta"],
"address": { "city": "London", "zip": "N1 9GU" },
"orders": [
{ "sku": "A-1", "qty": 2, "coupon": "SAVE10" },
{ "sku": "B-7", "qty": 1 }
]
}- Name of the main type
- Root
Generated code
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Address(
@SerialName("city") val city: String,
@SerialName("zip") val zip: String
)
@Serializable
data class Order(
@SerialName("sku") val sku: String,
@SerialName("qty") val qty: Long,
@SerialName("coupon") val coupon: String? = null
...About JSON to Kotlin Converter
Paste a sample of your JSON and get Kotlin data classes annotated for kotlinx.serialization, with nullable types and default values for optional fields. The sample can be an API response, a config file or a test fixture. When a list holds several objects they are merged, so a field that only some of them have is marked optional.
FAQs
What else can I generate from JSON?
Use JSON to Java Converter, or another converter in the JSON to code group.