Skip to content

JSON to Kotlin Data Classes Conversion — Numio

Generates Kotlin data classes from a JSON sample. Integers become Long, decimals Double, nested objects get their own class, and keys missing from some array items become nullable types such as Boolean?.

Modelling a REST response for an Android app that parses JSON with Jackson.

JSON

When to use it

  • Modelling a REST response for an Android app that parses JSON with Jackson.
  • Creating DTOs for a Ktor or Spring Boot service from an example payload.

How to use

  1. Paste a representative JSON object or array into the JSON editor; include array items that show optional keys.
  2. Read the Kotlin panel: the top-level class is named Root and nested classes are named from their keys (projects becomes Project).
  3. Copy the classes, rename Root, and replace Any? or Long where your API contract says otherwise.

JSON input

{
  "id": 1024,
  "full_name": "Ada Lovelace",
  "email": null,
  "roles": ["admin", "editor"],
  "projects": [
    { "name": "Engine", "stars": 42 },
    { "name": "Notes", "stars": 7, "archived": true }
  ]
}

Kotlin data classes

import com.fasterxml.jackson.annotation.JsonProperty

data class Root(
    val id: Long,
    @JsonProperty("full_name")
    val fullName: String,
    val email: Any?,
    val roles: List<String>,
    val projects: List<Project>,
)

data class Project(
    val name: String,
    val stars: Long,
    val archived: Boolean?,
)

Details and limitations

Conversion runs in your browser with the json_typegen WebAssembly module; the JSON is not uploaded. There are no settings on this page.

Keys that are not camelCase get a Jackson @JsonProperty annotation and the import com.fasterxml.jackson.annotation.JsonProperty. kotlinx.serialization or Moshi annotations are not generated.

null values and empty arrays carry no type information, so they become Any? and List<Any?>.

Nullable fields have no default value (= null). Add defaults yourself if the field can be absent when deserialising.

FAQ

Why is an integer typed as Long instead of Int?

The generator always uses Long for whole numbers so large IDs do not overflow. Numbers too large for a 64-bit integer, or written with a decimal point, become Double.

Can I convert a JSON array at the root?

Yes. A root array produces a typealias, for example typealias Root = List<Root2>;, plus a data class Root2 for the element type. Rename both to fit your code.

Why did a short mixed array become Pair<Long, String>?

json_typegen treats a short array whose items have different types as a tuple, so [1, "a"] becomes Pair<Long, String>. Change it to List<Any> if the array is really a list.

Specifications and references