Skip to content

JSON to Kotlin Data Class

Convert JSON to Kotlin data classes for kotlinx.serialization, Gson or Moshi, with nested classes and optional nullable fields.

JSON to Kotlin Data Class

Kotlin data class
import kotlinx.serialization.Serializable

@Serializable
data class User(
    var id: Int,
    var name: String,
    var email: String,
    var age: Int,
    var active: Boolean,
    var score: Double,
    var tags: List<String>,
    var address: Address,
    var metadata: String,
)

@Serializable
data class Address(
    var street: String,
    var city: String,
)

Kotlin data class generated

Need Go or Rust instead?JSON to Go →

Updated . Provided as is. Check the output before you rely on it in production.

How to use JSON to Kotlin Data Class

  1. 1

    Paste your JSON data

    Paste a JSON object or array. Strings become String, integers Int or Long, decimals Double, booleans Boolean and null Any?.

  2. 2

    Configure data class options

    Set the root class name, choose kotlinx.serialization, Gson or Moshi for the matching annotations and imports, and toggle nullable fields.

  3. 3

    Review and copy the code

    Nested JSON objects become separate data classes. Copy the output into your Kotlin project.

Questions and answers

What is a Kotlin data class?
A Kotlin data class holds data and gets equals(), hashCode(), toString() and copy() generated by the compiler, which suits API models. This tool turns sample JSON into data classes with Kotlin types, a class per nested object, and the annotations kotlinx.serialization, Gson or Moshi need.
What's the difference between the serialization libraries?
kotlinx.serialization is the official Kotlin multiplatform library with minimal reflection. Gson is Google's popular JSON serializer. Moshi is Square's modern alternative with better error messages. Each has different import statements and annotations, but all are widely supported.
What does @Serializable do?
@Serializable marks a class for kotlinx.serialization, whose compiler plugin generates the serializer at compile time. Gson needs no class annotation and uses @SerializedName for renamed keys; Moshi uses @JsonClass(generateAdapter = true) and @Json(name = ...) for renamed keys.
How do nullable fields work?
If you enable 'Nullable fields', all properties become nullable (String? instead of String). This means they can hold null values. This is useful when API responses have optional fields or you need to handle missing data gracefully.
Can I use the generated code directly?
Yes. Copy the generated Kotlin code into your Android or server project and add the serialization library to your build. Then parse JSON with Json.decodeFromString<MyClass>(json) for kotlinx.serialization, gson.fromJson(json, MyClass::class.java) for Gson, or a generated Moshi adapter.

Use it as an API

JSON to Kotlin Data Class is also callable as a free HTTP JSON API at https://aidevhub.io/api/json-to-kotlin/ — GET with query parameters or POST with a JSON body, no authentication, CORS enabled, fair use (abusive traffic is throttled at the edge; there is no per-request quota header). Responses return { ok, tool, result, meta }.

curl -s -X POST https://aidevhub.io/api/json-to-kotlin/ \
  -H "Content-Type: application/json" \
  -d '{"json":"{\"id\":1,\"name\":\"Alice\"}","root_name":"User"}'

Machine-readable contract: /api/tool/json-to-kotlin.json All API endpoints: /agents/ LLM site index: /llms.txt

For AI agents: how to call this tool

Machine-readable contract, endpoints and examples. Humans can ignore this section.

Best Path For Builders

Dedicated API endpoint

Deterministic outputs, machine-safe contracts, and production-ready examples.

Dedicated API

https://aidevhub.io/api/json-to-kotlin/

OpenAPI: https://aidevhub.io/api/openapi.yaml

GET /api/json-to-kotlin/ GET json-to-kotlin
POST /api/json-to-kotlin/ POST json-to-kotlin