vikasyadav53
vikasyadav53

Reputation: 109

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token

I got stuck in mapping between JSON to Java object. I am using RestTemplate to make third party API call in spring boot.

{
"parsed": [
        {
            "food": {
                "foodId": "<FoodID>",
                "label": "apple",
                "nutrients": {
                    "ENERC_KCAL": 52,
                    "PROCNT": 0.26,
                    "FAT": 0.17,
                    "CHOCDF": 13.81,
                    "FIBTG": 2.4
                },
                "category": "Generic foods",
                "categoryLabel": "food",
                "image": "<Image URL>"
            }
        }
    ]
}

I have created below classes for the same (with there getters and setters).

@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
    private static final long serialVersionUID = -1927057943962033354L;
    ParsesList parsed;
}
public class ParsesList implements Serializable{
    private static final long serialVersionUID = 5902036842725888370L;
    List<Parse> parse;
}
public class Parse implements Serializable{
    private static final long serialVersionUID = -8651880919902785186L;
    private Food food;  

}
public class Food implements Serializable{
    private static final long serialVersionUID = -5933882633489466961L;
    private String foodId;
    private String label;
    Nutrients NutrientsObject;
    private String brand;
    private String category;
    private String categoryLabel;
    private String foodContentsLabel;
    private String image;
}
public class Nutrients implements Serializable{
    private static final long serialVersionUID = -8207662942326982309L;
    private float ENERC_KCAL;
    private float PROCNT;
    private float FAT;
    private float CHOCDF;
    private float FIBTG;
}

But I'm getting below exception

[Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting 
response for type [class com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI] 
and content type [application/json;charset=UTF-8]; 
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
Cannot deserialize instance of 
`com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` 
out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY 
token at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain:
 com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])]
 with root cause

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain: com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.10.4.jar:2.10.4]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1445) ~[jackson-databind-2.10.4.jar:2.10.4]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1219) ~[jackson-databind-2.10.4.jar:2.10.4]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1171) ~[jackson-databind-2.10.4.jar:2.10.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1478) 

The added JSON is part of complete JSON. Please let me know if complete JSON required.

Upvotes: 0

Views: 8546

Answers (1)

dassum
dassum

Reputation: 5103

Couple of things you need to correct as per the JSON you have shared

@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
    private static final long serialVersionUID = -1927057943962033354L;
    List<Parse> parsed;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Food implements Serializable{
    private static final long serialVersionUID = -5933882633489466961L;
    private String foodId;
    private String label;
    Nutrients nutrients;
    private String brand;
    private String category;
    private String categoryLabel;
    private String foodContentsLabel;
    private String image;
}

Upvotes: 2

Related Questions