Use Default Values in Kotlin Data Classes for Missing JSON Fields

Learn how Kotlin default parameter values handle missing or optional JSON fields during deserialization. Covers coerceInputValues, encodeDefaults, and partial update patterns.

Serialization

Detailed Explanation

Default Values for Missing JSON Fields

Kotlin data classes support default parameter values, which pair naturally with JSON deserialization. When a field is missing from the JSON, the default value is used instead of throwing an error.

Example JSON (partial payload)

{
  "name": "Alice",
  "email": "alice@example.com"
}

Data Class with Defaults

@Serializable
data class User(
    val name: String,
    val email: String,
    val role: String = "viewer",
    val verified: Boolean = false,
    val loginCount: Int = 0,
    val bio: String? = null
)

Missing fields (role, verified, loginCount, bio) use their defaults. Present fields (name, email) are deserialized normally.

Json Configuration

val json = Json {
    ignoreUnknownKeys = true    // Skip JSON keys not in data class
    coerceInputValues = true    // Use default for null on non-nullable fields
    encodeDefaults = false      // Omit default values when serializing
}

coerceInputValues

When JSON sends null for a non-nullable field with a default:

{ "name": "Alice", "role": null }

Without coerceInputValues: throws a serialization exception. With coerceInputValues = true: uses the default value "viewer".

encodeDefaults

Controls whether default values appear in serialized output:

// encodeDefaults = false (default)
// Output: {"name":"Alice","email":"alice@example.com"}

// encodeDefaults = true
// Output: {"name":"Alice","email":"alice@example.com","role":"viewer","verified":false,"loginCount":0,"bio":null}

Partial Update Pattern

Defaults are especially useful for PATCH-style APIs:

@Serializable
data class UserUpdate(
    val name: String? = null,
    val email: String? = null,
    val bio: String? = null
)

// Only provided fields are non-null
val update = json.decodeFromString<UserUpdate>(patchBody)
if (update.name != null) updateName(update.name)

Defaults vs Nullable

  • val role: String = "viewer" -- always has a value; missing JSON uses default
  • val bio: String? = null -- may be null; represents "not set"
  • val bio: String? = null with default -- handles both missing and explicit null

Choose based on your domain: does "missing" mean "use default" or "not applicable"?

Use Case

Configuration endpoints and user preference APIs often return partial JSON where not all fields are present. Default values in Kotlin data classes handle this gracefully without littering the codebase with null checks or manual fallback logic.

Try It — JSON to Kotlin

Open full tool