json to kotlin data class converter

typetools

instant, client-side type converter • 0ms network latency

root:
inputjson
paste or drop file
307 chars
outputkotlin
*.kt
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

@Serializable
data class Metadata(
    val lastLogin: String,
    val ipAddress: String,
    val failedAttempts: JsonElement? = null
)

@Serializable
data class RootPayload(
    val userId: String,
    val username: String,
    val email: String,
    val isActive: Boolean,
    val score: Double,
    val roles: List<String>,
    val metadata: Metadata,
    val tags: List<JsonElement>
)
0.15ms•in-browser
documentation, edge cases & specifications below

step-by-step integration guide

technical specification differences & end-to-end conversion workflow

Null Safety at the JVM & Android Boundary: JSON vs. Kotlin Data Classes

Kotlin's type system was engineered around compile-time null safety, strictly separating non-null references (String, Int, List<T>) from nullable references (String?, Int?). When consuming external REST APIs on Android or Ktor/Spring Boot backends, runtime crashes (NullPointerException or MissingFieldException) occur when an incoming JSON payload contains an unexpected null value or omits a field that the Kotlin data class constructor declared as non-nullable without a default value. Additionally, Kotlin enforces camelCase property names while API payloads frequently use snake_case keys.

Synthesizing @Serializable Data Classes, @SerialName, and Nullable Defaults

Our JSON-to-Kotlin compiler inspects your raw JSON AST and emits idiomatic, immutable val properties inside concise data class declarations. When a JSON key uses snake_case or collides with a Kotlin keyword (such as val, in, object, or when), the generator converts the Kotlin property identifier to camelCase and attaches a @SerialName("original_key") (or @SerializedName) annotation. By scanning all sibling elements inside JSON arrays, the AST engine identifies any property that is null or conditionally absent and promotes its Kotlin type to nullable with a safe default parameter (e.g., val middleName: String? = null). This ensures kotlinx.serialization and Moshi can deserialize partial payloads without throwing missing-field exceptions.

frequently asked questions

common developer questions answered

q:Why should nullable Kotlin data class properties include '= null' default values?

In kotlinx.serialization and Moshi, marking a property as String? allows it to hold an explicit JSON null, but if the key is completely omitted from the JSON object, deserialization will still fail unless the constructor parameter has a '= null' default value.

q:How does the converter handle 64-bit integers vs. 32-bit Int in Kotlin?

The AST analyzer checks the magnitude of JSON integers: values within 32-bit signed bounds can use Int, while larger IDs or timestamps automatically promote to Kotlin's 64-bit Long type.

q:Are the generated data classes compatible with both kotlinx.serialization and Gson/Jackson?

Yes. The generated immutable val constructor properties work seamlessly across kotlinx.serialization, Moshi, Jackson (with KotlinModule), and Gson.

topic cluster

related type & ast converters

3 interconnected tools