Nullable, Default & Optional Fields
Handle missing or partial JSON gracefully.
Nullable, Default & Optional Fields is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
JSON Is Often Incomplete
Real responses skip fields or send null. Your shared models must handle missing data without crashing either app.
Make a Field Nullable
Mark a property as nullable with a trailing question mark, and the serializer happily reads a JSON null into it.
@Serializable
data class User(val bio: String?)Null vs Missing
A key set to null and a key that is simply absent are different cases, and the library treats each one distinctly.
Give a Default Value
A property with a default can be left out of the JSON entirely, and decoding fills in your fallback.
val role: String = "member"Optional Equals Default
In kotlinx.serialization, an optional field just means one that has a default, so absence is never an error.
Required Fields Are Strict
A non-null property with no default is required. If the JSON omits it, decoding throws a clear exception.
Nullable With a Default
You can combine both: a nullable property that defaults to null tolerates a missing key and a null value alike.
val avatar: String? = nullSkip Null on Output
Set explicitNulls to false and the encoder omits null fields instead of writing them out as null.
Json { explicitNulls = false }Tolerate Extra Keys
APIs add fields over time. Turn on ignoreUnknownKeys so new JSON keys never break your existing models.
Json { ignoreUnknownKeys = true }Coerce Bad Values
With coerceInputValues, a null sent for a field that has a default falls back to that default instead of failing.
Json { coerceInputValues = true }Resilient by Design
Combining nullable types, defaults and lenient config makes your shared parsing robust against messy real-world JSON.
Quick Check
How do you let a JSON key be safely omitted from a response?
Recap
Use nullable types and defaults for missing data, and tune Json with ignoreUnknownKeys, explicitNulls and coerceInputValues for resilient parsing.
Frequently asked questions
Is the “Nullable, Default & Optional Fields” lesson free?
Yes — the full text of “Nullable, Default & Optional Fields” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nullable, Default & Optional Fields”?
Handle missing or partial JSON gracefully. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nullable, Default & Optional Fields” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- @Serializable Data Models
- @SerialName & Custom Field Mapping
- Nullable, Default & Optional Fields
- Plug Serialization into Ktor