JSON To Kotlin Generator

JSON Input

Loading editor…

Generated Kotlin

Configuration

Enter JSON data to generate Kotlin data classes

Supports data classes, kotlinx.serialization and null safety

How to convert JSON to Kotlin – step-by-step guide

Use this JSON to Kotlin generator to create Kotlin data classes from JSON samples for Android and JVM backend projects.

  1. Step 1 – Paste a JSON sample

    • Paste a representative JSON object or array into the left editor.
    • Include nested objects, arrays, and nullable fields so types are inferred correctly.
    • Use Import to load JSON from a file, URL, or sample data.
  2. Step 2 – Choose Kotlin data class options

    • Set a root Class Name for the generated data class.
    • Decide how nullable fields should be represented (String?, Double?).
    • If you use serialization libraries, align with your stack (Moshi/Kotlinx/Jackson).
  3. Step 3 – Review the generated code

    • Check field names, types, and how arrays/objects are modeled.
    • Adjust options like Root Type Name, null handling, and frameworks if available.
    • If a field is inferred incorrectly, tweak your sample JSON and regenerate.
  4. Step 4 – Use the data classes in Kotlin

    • Paste the data classes into your project package.
    • Deserialize JSON into the root type with your chosen library.
    • Keep models versioned when your API changes frequently.
  5. Step 5 – Copy or download

    • Copy the output into your project or download it as a file.
    • Run your formatter/linter to match your code style.
    • Add JSON parsing/serialization libraries if your language requires them.

Quick tips

  • Prefer immutable val properties for safer models.
  • Use explicit @Json/@SerialName annotations when field names differ.
  • Generate JSON Schema when you need shared contracts across teams.
Example output (simplified)
// JSON input
{
  "id": 123,
  "name": "Maeve Winters",
  "email": "maeve@example.com",
  "active": true,
  "roles": ["admin", "editor"],
  "metadata": { "plan": "pro" },
  "createdAt": "2024-03-01T10:15:00Z",
  "score": 99.5,
  "notes": null
}

// Generated Kotlin data classes (simplified)
data class Metadata(
  val plan: String
)

data class Root(
  val id: Int,
  val name: String,
  val email: String?,
  val active: Boolean,
  val roles: List<String>,
  val metadata: Metadata,
  val createdAt: String,
  val score: Double,
  val notes: Any?
)

Related JSON & Kotlin tools

Explore more JSON and schema tools that work great alongside this JSON to Kotlin generator.

Frequently Asked Questions

What Kotlin features are supported?

The generator creates Kotlin data classes with support for kotlinx.serialization, Gson, and Jackson annotations for JSON serialization, plus modern Kotlin language features like null safety and data classes.

Can I generate kotlinx.serialization compatible classes?

Yes! Choose 'kotlinx' as the framework to generate classes with @Serializable and @SerialName annotations for type-safe JSON serialization support.

How does Gson and Jackson integration work?

When selecting Gson or Jackson frameworks, the generator adds appropriate annotations (@SerializedName or @JsonProperty) to fields for proper JSON serialization/deserialization.

How is package structure handled?

You can specify a custom package name in the options. The generator will add the appropriate package declaration at the top of the generated Kotlin file.

JSON to Kotlin Data Class Generator Online | JSONSwiss