This episode covers data binding and JSON: the process of JSON serialization and deserialization in Java, the popular libraries Jackson, Gson, and JSON-B, mapping Java objects to JSON and back, and validating input data plus error handling while parsing.

JSON is the most common data exchange language in the modern world. Almost every REST API, message broker, and data store speaks this format. Episode 9 covers data binding and JSON processing in Java — how to convert Java objects into JSON and back.
You will get to know three popular libraries: Jackson, Gson, and JSON-B. In addition, you will learn to map objects correctly, validate input data, and handle errors while parsing so your application stays stable.
Serialization is the process of converting a Java object into a JSON string. Deserialization is the reverse — converting a JSON string into a Java object.
objek Java -> serialisasi -> JSON -> deserialisasi -> objek JavaAll examples in this episode use the concise Pengguna record:
public record Pengguna(String nama, int umur) { }Jackson is the most popular JSON library and the default in Spring Boot. Add the dependency, then use ObjectMapper:
mvn dependency:get -Dartifact=com.fasterxml.jackson.core:jackson-databind:2.17.2import com.fasterxml.jackson.databind.ObjectMapper;
public class DemoJackson {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(new Pengguna("Arman", 30));
System.out.println(json);
Pengguna hasil = mapper.readValue(json, Pengguna.class);
System.out.println(hasil.nama());
}
}mapper.writeValueAsString(pengguna) produces JSON, and mapper.readValue(json, Pengguna.class) converts it back into an object.
Gson offers a concise API:
import com.google.gson.Gson;
public class DemoGson {
public static void main(String[] args) {
Gson gson = new Gson();
String json = gson.toJson(new Pengguna("Budi", 25));
System.out.println(json);
Pengguna hasil = gson.fromJson(json, Pengguna.class);
System.out.println(hasil.umur());
}
}gson.toJson(pengguna) and gson.fromJson(json, Pengguna.class) give easy data binding.
JSON-B is the standard Jakarta EE specification. Its advantage is portability across implementations such as Eclipse Yasson and Apache Johnzon:
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
public class DemoJsonb {
public static void main(String[] args) {
Jsonb jsonb = JsonbBuilder.create();
String json = jsonb.toJson(new Pengguna("Citra", 28));
System.out.println(json);
}
}Sometimes the field names in JSON differ from the Java fields. Use annotations — Jackson uses @JsonProperty:
import com.fasterxml.jackson.annotation.JsonProperty;
public class Produk {
@JsonProperty("nama_produk")
private String nama;
@JsonProperty("harga_jual")
private double harga;
}Validate data after deserialization with Jakarta Bean Validation:
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Min;
public class OrderRequest {
@NotBlank
private String customerId;
@Min(1)
private int jumlah;
public String getCustomerId() { return customerId; }
public int getJumlah() { return jumlah; }
}Invalid JSON or mismatched fields trigger exceptions. Handle them correctly:
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;
public class DemoError {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String jsonBuruk = "{\"nama\":";
try {
Pengguna hasil = mapper.readValue(jsonBuruk, Pengguna.class);
System.out.println(hasil.nama());
} catch (JsonProcessingException e) {
System.out.println("Gagal parsing: " + e.getMessage());
}
}
}JsonProcessingException is the Jackson-specific exception for parsing errors. Gson uses JsonSyntaxException, and JSON-B uses JsonbException. Never assume external JSON is already correct — validate and catch exceptions at the right layer.
Episode 9 teaches data binding and JSON: understanding serialization and deserialization, using Jackson, Gson, and JSON-B, mapping fields with annotations, validating input, and handling parsing errors.
Key takeaways:
gson.toJson and fromJson.@JsonProperty annotation maps differing field names.In the next episode, episode 10, we will discuss database access and persistence — basic JDBC with connections, statements, and result sets, connection pooling and resource cleanup, an introduction to JPA and Hibernate as modern ORMs, entity mapping, the repository pattern, and transaction management. Time to store data persistently!