javakotlinjsonjacksonspring-bootandroid

JSON to Java and Kotlin: Generate POJOs and Data Classes from API Responses

·8 min read

Deserializing JSON in Java and Kotlin

When you call a REST API in Java or Kotlin, the response comes back as a JSON string. To work with it in a type-safe way, you need model classes that match the JSON structure. Jackson, the de-facto JSON library for the JVM, deserializes the string into your objects automatically once you define those classes.

Writing these classes by hand for complex API responses is tedious. Use JSONKit's generators to produce them instantly: - JSON to Java POJO: /json-to-java - JSON to Kotlin Data Class: /json-to-kotlin

Java with Jackson

Given this JSON API response:

json
{
  "user_id": 1,
  "first_name": "Ravi",
  "is_verified": true,
  "address": {
    "city": "Surat",
    "pin_code": "395001"
  }
}

Jackson mode generates:

java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Address {
    @JsonProperty("pin_code")
    private String pinCode;
    private String city;

    public String getPinCode() { return pinCode; }
    public void setPinCode(String pinCode) { this.pinCode = pinCode; }
    public String getCity() { return city; }
    public void setCity(String city) { this.city = city; }
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Root {
    @JsonProperty("user_id")
    private Long userId;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("is_verified")
    private Boolean isVerified;
    private Address address;

    // getters and setters...
}

Deserialize it:

java
ObjectMapper mapper = new ObjectMapper();
Root user = mapper.readValue(jsonString, Root.class);
System.out.println(user.getFirstName()); // Ravi

Java with Lombok

Lombok eliminates the getters/setters boilerplate. Add the Lombok dependency and use @Data:

java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Root {
    @JsonProperty("user_id")
    private Long userId;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("is_verified")
    private Boolean isVerified;
    private Address address;
}

Lombok generates all getters, setters, equals(), hashCode(), and toString() at compile time. No IDE plugin needed for it to work at runtime.

Kotlin Data Class

Kotlin's data class is even cleaner — it handles all boilerplate natively:

kotlin
import com.fasterxml.jackson.annotation.JsonProperty

data class Address(
    val city: String,
    @JsonProperty("pin_code") val pinCode: String
)

data class Root(
    @JsonProperty("user_id") val userId: Long,
    @JsonProperty("first_name") val firstName: String,
    @JsonProperty("is_verified") val isVerified: Boolean,
    val address: Address
)

Deserialize with Kotlin Jackson module:

kotlin
val mapper = ObjectMapper().registerModule(KotlinModule.Builder().build())
val user: Root = mapper.readValue(jsonString)
println(user.firstName) // Ravi

Spring Boot — Zero Config

In Spring Boot, Jackson is auto-configured. You can inject ObjectMapper directly or use the RestTemplate / WebClient which deserialize automatically:

kotlin
// WebClient (reactive)
val user = webClient.get()
    .uri("/users/1")
    .retrieve()
    .bodyToMono<Root>()
    .awaitFirst()

// RestTemplate
val user = restTemplate.getForObject("/users/1", Root::class.java)

Type Mapping Reference

JSONJavaKotlin
stringStringString
integerLongLong
floatDoubleDouble
booleanBooleanBoolean
nullObjectAny?
arrayList<T>List<T>
objectClassdata class

Pro Tips

  1. Always add @JsonIgnoreProperties(ignoreUnknown = true) so your classes survive API additions without breaking.
  2. Use Long not int for IDs — APIs sometimes return large integers that overflow 32-bit integers.
  3. For dates, use String in the model class and parse with LocalDateTime.parse() — Jackson's @JsonFormat can do it automatically but requires configuring the mapper.

Try JSON to Java / Kotlin

Generate Java POJOs or Kotlin data classes from any JSON in seconds.