JSON to Kotlin Data Class Converter

Paste JSON to generate Kotlin data classes with serialization annotations and full nested type inference.

About This Tool

The JSON to Kotlin converter is a free browser-based tool that automatically generates Kotlin data classes from any JSON structure. Instead of manually writing data class definitions for API responses, configuration files, or database records, you can paste the raw JSON and get accurate, ready-to-use Kotlin code in milliseconds.

The converter performs recursive type inference across your entire JSON structure. Primitive values are mapped to their corresponding Kotlin types — String, Int, Long, Double, and Boolean. Nested objects are extracted into their own named data classes, with names derived from the field key using PascalCase conversion. Arrays are analyzed to determine a consistent element type, producing List<T> for homogeneous arrays or List<Any> for mixed content. Null values are handled gracefully by generating nullable types with the ? suffix.

You can choose between four serialization annotation modes: kotlinx.serialization (the official Kotlin serialization library), Gson (widely used in Android), Moshi (a modern JSON library for Kotlin), or no annotations at all. When annotation support is enabled, snake_case JSON keys are automatically mapped to idiomatic camelCase Kotlin property names with the appropriate annotation to preserve the original key mapping.

All processing happens entirely in your browser. Your JSON data never leaves your machine — there are no server round-trips, no logging, and no third-party analytics on your input. This makes it safe for sensitive payloads such as internal API responses, authentication tokens, and configuration files that contain secrets.

Additional options let you customize the output to match your project's coding standards. You can toggle between val and var properties, enable nullable types for all fields, set the root class name, and optionally generate a companion object with a sample JSON string for documentation or testing purposes. The generated code includes all necessary import statements so you can paste it directly into your Kotlin project.

How to Use

  1. Paste or type your JSON into the JSON Input panel on the left.
  2. The Kotlin output updates automatically in the right panel as you type.
  3. Set the Root class name field to customize the top-level data class name (defaults to "Root").
  4. Select a serialization library from the dropdown to add the appropriate annotations (kotlinx.serialization, Gson, Moshi, or None).
  5. Use the toggle buttons to enable nullable types, switch between val/var, or generate a companion object with sample JSON.
  6. Click Copy or press Ctrl+Shift+C to copy the generated Kotlin code to your clipboard.

Popular JSON to Kotlin Examples

View all JSON to Kotlin examples →

FAQ

How does the tool handle nested JSON objects?

Each nested JSON object is extracted into its own Kotlin data class. The class name is derived from the field key using PascalCase conversion. For example, a field called "shipping_address" produces a ShippingAddress data class. If a name collision occurs, a numeric suffix is appended automatically (Item, Item2, Item3, etc.).

Which serialization library should I choose?

kotlinx.serialization is the official Kotlin serialization library and works well with Kotlin Multiplatform projects. Gson is a popular choice for Android projects using Java interop. Moshi is a modern alternative that handles Kotlin features like nullability better than Gson. Choose "None" if you do not need serialization annotations.

How are snake_case JSON keys handled?

JSON keys written in snake_case are automatically converted to camelCase property names, which is the idiomatic Kotlin convention. A @SerialName, @SerializedName, or @Json annotation is added to preserve the mapping to the original JSON key name.

What happens with arrays of mixed types?

If an array contains elements of different types, the converter generates List<Any> to safely represent the mixed content. If all elements share the same type, a strongly typed List<T> is produced. Empty arrays default to List<Any>.

Is my data safe?

Yes. All code generation runs entirely in your browser using JavaScript. No data is sent to any server. You can verify this by checking the Network tab in your browser's developer tools while using the tool.

What is the difference between val and var?

In Kotlin, val declares an immutable (read-only) property, while var declares a mutable property. Using val is the recommended default because it promotes immutability and safer code. Use var when you need to modify the property after construction.

How does the companion object toggle work?

When enabled, the root data class includes a companion object with a sample JSON string constant. This is useful for documentation, testing, or providing a quick reference for the expected JSON structure alongside the data class definition.

Related Tools