I am working on a Java project which parses a JSON response received from an external API using the Jackson library. One of the fields in the response sometimes comes as a single object and in certain cases, it comes as an array of objects. So I'm not sure which datatype I should select to map this response back into a Java Object. How should I properly map both response types to a Java object?
In the possible duplicate mentioned above, the response is always a list but in my case its not. So I dont think its the duplicate of above issue.
Below is the response I'm receiving:
"configuration": {
"additionalServices": {
"type": "Standard DDOS IP Protection"
},
}
And sometimes this is how I receive the same response:
"configuration": {
"additionalServices": [
{
"type": "Standard DDOS IP Protection"
},
{
"type": "Remote Management"
}
],
}
This is how my Java mapping looks like now:
@JsonIgrnoreProperties(ignoreUnknown = true)
public class Configuration {
private List<AdditionalServices> additionalServices;
}
@JsonIgrnoreProperties(ignoreUnknown = true)
public class AdditionalServices {
private String type;
}
If I use the below declaration then it will parse only the array output and throws exception for the first response:
private List<AdditionalServices> additionalServices;
If I use the below declaration then it will parse only the first response and throws an exception for the second response:
private AdditionalServices additionalServices;
Exception in parsing the data:
Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token