Parsing JSON with Moshi
Map responses into Kotlin data classes.
Parsing JSON with Moshi is a free Jetpack Compose Academy lesson on CoddyKit — lesson 2 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why You Need a Parser
The server sends raw JSON text, but your UI needs real Kotlin objects. A parser like Moshi bridges that gap automatically.
Map JSON to a Data Class
Each JSON object becomes a Kotlin data class. Moshi matches field names and fills in the properties for you. ✨
data class User(
val id: Int,
val name: String
)Use @JsonClass for Codegen
Add @JsonClass with generateAdapter true so Moshi builds a fast adapter at compile time instead of using slow reflection.
@JsonClass(generateAdapter = true)
data class User(val id: Int, val name: String)Rename Fields With @Json
When a JSON key uses snake_case, the @Json annotation maps it to a clean Kotlin property name without renaming the API.
@Json(name = "full_name")
val fullName: StringNullable Means Optional
If a field might be missing, make it nullable with a question mark. Moshi sets it to null instead of crashing on absent keys.
data class User(
val id: Int,
val bio: String?
)Default Values Are Handy
Give a property a default value and Moshi uses it whenever the JSON omits that field, keeping your model resilient to change.
val isVerified: Boolean = falseNested Objects Just Work
If your JSON nests another object, model it as its own data class. Moshi parses the whole tree in one pass.
data class User(val id: Int, val address: Address)Lists of Objects
A JSON array maps cleanly to a Kotlin List of your data class. Moshi builds every item for you in order.
@GET("users")
fun getUsers(): Call<List<User>>Build a Moshi Instance
Create one shared Moshi object and add the Kotlin adapter factory so it understands data classes properly.
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()Plug Moshi Into Retrofit
Pass your Moshi instance to MoshiConverterFactory when building Retrofit, and every response is parsed without extra code.
.addConverterFactory(MoshiConverterFactory.create(moshi))Keep Models Focused
Only model the fields you actually use. Moshi ignores unknown JSON keys, so a lean data class stays clean and fast.
Quick Check
How do you map a JSON key named first_name to a Kotlin property called firstName?
Recap: JSON Into Objects
You modeled responses as data classes, handled renames, nulls, and nesting, and wired Moshi into Retrofit. Next you'll call these endpoints from a ViewModel.
Frequently asked questions
Is the “Parsing JSON with Moshi” lesson free?
Yes — the full text of “Parsing JSON with Moshi” is free to read here on the web, and the Jetpack Compose 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.
What will I learn in “Parsing JSON with Moshi”?
Map responses into Kotlin data classes. You practise Jetpack Compose 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 Jetpack Compose Academy?
No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parsing JSON with Moshi” 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 Jetpack Compose Academy lesson?
Yes. Every Jetpack Compose 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
- Define a Retrofit API Interface
- Parsing JSON with Moshi
- suspend Calls in the ViewModel
- Loading, Success & Error UI