0

I'm consuming a REST API that can answer in this manner:

[
   {
      "status":"1",
      "report":{
         "name":"John",
         "job":"Software Developer"
      }
   },
   {
      "status":"0",
      "report":"John not found"
   }
]

As you can see above the value of report field can be both a JSON Object and a String. This is the reason why I am in trouble parsing it as POJO.

For instance I have my OutcomeResponse POJO:

public class OutcomeResponse {

   private String status;
   private Report report;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        this.report = report;
    }
}

Where Report is:

public class Report {

   private String name;
   private String job;
   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

Now I'm executing a POST as follow:

OutcomeResponse [] response = client.executePost(endpoint, headers, body, OutcomeResponse [].class);

But I have an exception: I have a string in report instead of an Object as expected:

Expected BEGIN_OBJECT but was STRING at line 1 column 25 path $[0].value

How can I solve this problem?

harry-potter
  • 1,981
  • 5
  • 29
  • 55
  • Do you have control over the API? You an ensure that your json response always contains an object. Or you can refer to https://stackoverflow.com/questions/37953102/json-value-is-sometimes-a-string-and-sometimes-an-object, You can probably do something similar with java. – Jawad El Fou Oct 15 '20 at 15:50
  • No, sadly I can't edit the API. – harry-potter Oct 15 '20 at 15:52

0 Answers0