JSON to Kotlin Data Class

JSON Input
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,
)
Need Go or Rust instead?JSON to Go →

What This Tool Does

JSON to Kotlin Data Class is built for deterministic developer and agent workflows.

Convert JSON to Kotlin data classes with @Serializable annotations. Supports kotlinx.serialization, Gson, and Moshi.

Use How to Use for execution steps and FAQ for constraints, policies, and edge cases.

Last updated:

This tool is provided as-is for convenience. Output should be verified before use in any production or critical context.

Agent Invocation

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

Unified Runtime API

https://aidevhub.io/api/tools/run/?toolId=json-to-kotlin&a=...

GET and POST are supported at /api/tools/run/ with identical validation and limits.

Limit: req / s, input max 128 KB.

How to Use JSON to Kotlin Data Class

  1. 1

    Paste your JSON data

    Input a JSON object or array in the left panel. The tool parses the structure and infers Kotlin types: strings become String, integers become Int or Long, decimals become Double, booleans become Boolean, and null becomes Any?.

  2. 2

    Configure data class options

    Set the root class name (default: Root). Toggle nullable types for optional fields. Choose between data classes and regular classes. Enable @SerializedName annotations for custom JSON key mapping.

  3. 3

    Review generated Kotlin code

    The right panel shows idiomatic Kotlin data classes with proper type annotations. Nested JSON objects become separate named data classes. Copy the output directly into your Kotlin project.

Frequently Asked Questions

What is a Kotlin data class?
A Kotlin data class is a class that holds data with automatic equals(), hashCode(), toString(), and copy() methods. They're ideal for modeling API responses, database entities, and immutable data structures. Use the 'data class' keyword to define them.
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 is an annotation that marks a class as serializable for JSON conversion. With kotlinx.serialization, the compiler generates serialize/deserialize code at compile time. Other libraries use different annotations (@Serializable for Moshi requires @Json field annotations).
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. Add the appropriate serialization library to your build.gradle, then use the generated data classes to parse JSON with MyClass.serializer().deserialize(jsonString) or your library's equivalent method.