0

I am having trouble in deserialising the below JSON String to a Java Object.

{
    "action": {
               "onWarning":{
                   "alert":{
                          "isEnabled": true,
                          "name": ""
                           }
                   },
               "onError":{
                 "alert":{
                        "isEnabled": true,
                        "name": ""
                        }
                   },
                "onSuccess":{
                 "alert":{
                        "isEnabled": true,
                        "name": ""
                        }
                   }
             }
}

The code below should work but i think my issue is with the mapper i am passing with it -

ObjectMapper mapper = new ObjectMapper();
JobConfig lib = mapper.readValue(jsonString, JobConfig.class);
System.out.println(lib.action.onWarning.alert.isEnabled);

The mapper i pass with it is like below :

Alert.java

public class Alert{
    @JsonProperty("isEnabled")
    public boolean isEnabled;

    @JsonProperty("name")
    public String name;
}

AlertConfig.java

public class AlertConfig {
    
    @JsonProperty("onSuccess")
    public Alert onSuccess;

    @JsonProperty("onWarning")
    public Alert onWarning;

    @JsonProperty("onError")
    public Alert onError;
}

JobConfig.java

public class JobConfig {
    @JsonProperty("action")
    public AlertConfig action;
}

Error i get is :

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "alert" (class com.test.dv.service.domain.Alert), not marked as ignorable (2 known properties: "name", "isEnabled"])

I am nor sure how to name the content of alert JSON as alert but it already has been named as Alert.

2
  • 2
    You're missing a level on between AlertConfig which has fields like onSuccess and Alert which has isEnabled. What should be mapped to the alert in between? Commented Aug 30, 2021 at 12:02
  • You can introduce AlertWrapper class that holds Alert object, then AlertConfig should consists of AlertWrapper instead of just Alert Commented Aug 30, 2021 at 12:02

2 Answers 2

2

the error is right, in your DTOs you the onError/Success/Warning fields are of types Alert, type alert has name and isEnabled, you don't have the alert field as in the json

"onError":{
    "alert":{

You are missing a DTO which has the property alert and the onError/Success/Warning should be of that type (or remove the alert property from the json)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, You are right, What would be the best way to resolve this issue? I still want to have the "alert" in my JSON, so should i create a new DTO but what would that look like as i already have Alert.java
1

You should create an AlertWrapper Class like below -

public class AlertWrapper {

    @JsonProperty("alert")
    public Alert alert;
}

And your AlertConfig class should contain AlertWrapper like below -

public class AlertConfig {

    @JsonProperty("onSuccess")
    public AlertWrapper onSuccess;

    @JsonProperty("onWarning")
    public AlertWrapper onWarning;

    @JsonProperty("onError")
    public AlertWrapper onError;
}

Doing this you will complete your DTO(s)

2 Comments

Thanks, I understood now.. This works for me, however i am just wondering now if this is a good way to acieve this? In scala i would create case classes all in one file but in java it just get's too messy with soo many POJOs
There can be better ways to do this, this is also not a bad way to do what you are doing. People try to write code differently and every way is good in it's own way. Scala for sure gives better ways to do things.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.