DEV Community

Jasper Aurelio Villas
Jasper Aurelio Villas

Posted on

JSON mapping issue in JAVA

property declaration is

private Boolean isActive;
Enter fullscreen mode Exit fullscreen mode

but json reads only


  "active": true
Enter fullscreen mode Exit fullscreen mode

ChatGPT zei:
Ah — you've hit a common JSON mapping issue in Java!

🧠 Problem:
Your property in the class is named:

java

private Boolean isActive;
Enter fullscreen mode Exit fullscreen mode

But your JSON is sending:

json

"active": true
Enter fullscreen mode Exit fullscreen mode

By default, most serializers/deserializers (like Jackson, used in Spring Boot) map the field name based on the JavaBean getter/setter. In your case:

java

public Boolean getActive() {
    return isActive;
}
public void setActive(Boolean active) {
    isActive = active;
}
Enter fullscreen mode Exit fullscreen mode

Because the method is named getActive() (not getIsActive()), Jackson expects the JSON key to be active, not isActive — which is why the JSON works only if it's active, not isActive.

✅ Best Practice: Rename the field
To avoid confusion and be consistent, change your field to:

java

private Boolean active;
Enter fullscreen mode Exit fullscreen mode

Then everything aligns naturally:

Field name: active

JSON key: "active"

Getter/setter: getActive() / setActive(Boolean)

🚫 If you must keep isActive as the field name
You can use Jackson annotations to map it explicitly:

java

@JsonProperty("active")
private Boolean isActive;
Enter fullscreen mode Exit fullscreen mode

But this adds unnecessary complexity if you're not relying on isX naming for semantic reasons.

✅ Recommendation:
Rename your field to active, unless there's a strong reason to keep isActive.

Top comments (0)