Json2POJOJson2POJO
Android & Kotlin Guide

Android JSON to POJO

Stop writing boilerplate. Embrace Kotlin Data Classes, switch to Moshi, and optimize network calls with Retrofit.

The Kotlin Advantage

In the old days of Java (pre-Java 14 Records), a simple POJO required 50+ lines of getters, setters, `equals`, `hashCode`, and `toString`. In Kotlin, it's one line.

The Old Way (Java)

public class User {
    private String name;
    private int age;
    // ... getters
    // ... setters
    // ... equals
    // ... hashCode
    // ... toString
}

The Modern Way (Kotlin)

data class User(
    val name: String,
    val age: Int
)

That's it. data class automatically gives you standard POJO methods and copy() functionality.

Battle of the Parsers: Moshi vs Gson

For years, Gson was the king. But for modern Android development, Moshi is superior.

What is Moshi? Moshi is a modern JSON library for Android and Java that seamlessly handles Kotlin's null-safety and integrates directly with Okio for high performance.

It is the perfect companion for consuming Spring Boot APIs. Why?

  • Kotlin Support: Moshi understands Kotlin's non-nullable types. Gson uses `Unsafe` and can set a non-null `String` to `null`, causing crashes later.
  • Size: Moshi is smaller and has a cleaner API.
  • Performance: Moshi uses Okio for faster I/O operations.

Retrofit Integration: Making REST Calls

When "making a rest call converting json to pojo", you don't parse manually. You let Retrofit do it.

NetworkModule.kt
// 1. Create the Moshi instance
val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

// 2. Add it to Retrofit
val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .addConverterFactory(MoshiConverterFactory.create(moshi)) // The Magic
    .build()

// 3. Define your API
interface ApiService {
    @GET("users")
    suspend fun getUsers(): List<User> // Returns POJOs directly!
}

Essential Edge Cases (Don't Crash in Prod!)

1. The R8/ProGuard Crash

When you build a Release APK, R8 shrinks your code and renames fields (obfuscation). This breaks JSON reflection because `user_server_id` cannot be found on class `a.b.c`.

The Fix: @JsonClass or Rules

Option A (Best): Use Code Gen

@JsonClass(generateAdapter = true)
data class User(...)

Option B: ProGuard Rules

-keep class com.myapp.models.** { *; }

2. Passing Data Between Activities

You cannot pass a raw POJO via `Intent`. You need it to be `Parcelable`. In Kotlin, just add the annotation:

@Parcelize
data class User(val name: String) : Parcelable

Requires `id('kotlin-parcelize')` plugin.

Android FAQ & Troubleshooting

How to generate POJO from JSON in Android Studio?

You can use the "RoboPOJO" plugin, but it often lags behind latest Moshi/Kotlin versions. Our online tool is faster and supports newer features like Java Records and Lombok.

How to convert JSON Object to POJO in Android?

Use Moshi (recommended) or Gson. With Moshi: Moshi moshi = new Moshi.Builder().build(); JsonAdapter<User> adapter = moshi.adapter(User.class); User user = adapter.fromJson(jsonString);

Should I use Gson or Moshi in 2025?

Use Moshi. It has better Kotlin support, smaller APK size, and predictable exception handling compared to Gson.

Ready to Generate Android Code?

Our tool supports Kotlin Data Classes, Moshi, and Parcelable out of the box.