Boneyard Tools

JSON to Kotlin Data Class Converter

Paste a JSON object and get ready to use Kotlin data classes with a type inferred for every field. Whole numbers become Int, decimals Double, arrays List, and each nested object turns into its own class named in PascalCase after its key. It runs entirely in your browser, so nothing you paste is sent to a server.

How to convert JSON to Kotlin data classes

  1. Paste a JSON object into the JSON object box, or click Load sample to see the format.
  2. Set the Root class name field if you want something other than Root.
  3. Watch the Kotlin data classes appear in the output box as you type.
  4. Fix any error shown in red, such as invalid JSON or a non-object root.
  5. Click Copy and paste the declarations straight into your Kotlin project.

Examples

Mixed field types with a nested object

{"id":1,"name":"Ada","active":true,"score":3.5,"tags":["dev","math"],"address":{"city":"Oslo","zip":null}}
data class Root(
    val id: Int,
    val name: String,
    val active: Boolean,
    val score: Double,
    val tags: List<String>,
    val address: Address
)

data class Address(
    val city: String,
    val zip: Any?
)

Array of objects becomes List of a generated class

{"items":[{"sku":"A1","qty":2}]}
data class Root(
    val items: List<Items>
)

data class Items(
    val sku: String,
    val qty: Int
)

Custom root class name

{"userName":"ada","age":30}
data class Account(
    val userName: String,
    val age: Int
)

Frequently asked questions

Is my JSON uploaded anywhere?

No. The conversion runs entirely in your browser with JavaScript, so the JSON you paste never leaves your device and nothing is stored on a server.

How are field types inferred?

Whole numbers become Int, fractional numbers Double, text String, true or false Boolean, arrays List of the first item's type, nested objects their own class, and JSON null the nullable Any?.

How are nested objects handled?

Each nested object becomes its own data class named in PascalCase after the key that held it, and the parent class references it by that type name. Parents are always printed before the classes they contain.

How are arrays typed?

A list takes the type of its first element, so a string array becomes List<String> and an array of objects becomes List of a generated class. An empty array cannot be inspected, so it falls back to List<Any?>.

Are property names changed to match Kotlin style?

No. Keys are kept exactly as written, so a JSON key of user_name stays val user_name. If your API uses snake_case, add kotlinx.serialization and a @SerialName annotation to map it to a camelCase property yourself.

Does it add @Serializable or Gson annotations?

No. The output is plain Kotlin data classes with no annotations or imports, so you can drop them in and add whichever serialization library you prefer.

What happens with a null value?

A null value is typed as the nullable Any?, since a concrete type cannot be inferred from null alone. If other responses fill that field, widen the type by hand, for example to String?.

Why are large numbers still typed as Int?

Any whole number is inferred as Int, so a value larger than a 32 bit Int can overflow. If a field holds big ids or timestamps, change its type to Long after pasting.

Why must the input be a JSON object?

Data classes model named fields, so the converter needs an object at the root. A bare array, string or number has no field names and cannot become a data class, so it reports an error.

Learn more

Related tools