Search code examples
javaspringresttemplate

How to fix Can not deserialize instance of Object[] out of START_OBJECT token


I'm trying to retrieve object from RestTemplate but everytime JsonMappingException is occuring. Is that something with class that I'm trying to map json object on? Looks like the array on the beginning could be a problem, but I can't figure this out.

Caused by: com.fasterxml.jackson.databind.JsonMappingException: 
Can not    deserialize instance of com.lubaszak.bean.ProductInfo[] out
of START_OBJECT token
at [Source: java.io.PushbackInputStream@10a3c667; line: 1, column: 1]


  public ResponseEntity<ProductInfo[]> getProductByQuery(@PathVariable    String query) {
    HttpEntity<?> httpEntity = headersProvider.getHeaders();

    ResponseEntity<ProductInfo[]> product =  restConfig.createRestTemplate()
            .exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductInfo[].class, query);
    return product;

Json object:

"branded": [
    {
        "food_name": "Big Mac",
        "serving_unit": "burger",
        "nix_brand_id": "513fbc1283aa2dc80c000053",
        "brand_name_item_name": "McDonald's Big Mac",
        "serving_qty": 1,
        "nf_calories": 540,
        "photo": {
            "thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
            "highres": null,
            "is_user_uploaded": false
        },
        "brand_name": "McDonald's",
        "region": 1,
        "brand_type": 1,
        "nix_item_id": "513fc9e73fe3ffd40300109f",
        "locale": "en_US"
    },

Java class:

@JsonIgnoreProperties(ignoreUnknown = true)

public class ProductInfo {

@JsonProperty("food_name")
public String foodName;
@JsonProperty("serving_unit")
public String servingUnit;
@JsonProperty("nix_brand_id")
public String nixBrandId;
@JsonProperty("brand_name_item_name")
public String brandNameItemName;
@JsonProperty("serving_qty")
public Integer servingQty;
@JsonProperty("nf_calories")
public Integer nfCalories;
@JsonProperty("brand_name")
public String brandName;
@JsonProperty("brand_type")
public Integer brandType;
@JsonProperty("nix_item_id")
public String nixItemId;

//getter methods
}

Solution

  • The above JSON is JSONArray of JSONObject, i believe actual format is

      { 
         "branded": [
    {
        "food_name": "Big Mac",
        "serving_unit": "burger",
        "nix_brand_id": "513fbc1283aa2dc80c000053",
        "brand_name_item_name": "McDonald's Big Mac",
        "serving_qty": 1,
        "nf_calories": 540,
        "photo": {
            "thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
            "highres": null,
            "is_user_uploaded": false
        },
        "brand_name": "McDonald's",
        "region": 1,
        "brand_type": 1,
        "nix_item_id": "513fc9e73fe3ffd40300109f",
        "locale": "en_US"
    },
        {
        "food_name": "Big Mac",
        "serving_unit": "burger",
        "nix_brand_id": "513fbc1283aa2dc80c000053",
        "brand_name_item_name": "McDonald's Big Mac",
        "serving_qty": 1,
        "nf_calories": 540,
        "photo": {
            "thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
            "highres": null,
            "is_user_uploaded": false
        },
        "brand_name": "McDonald's",
        "region": 1,
        "brand_type": 1,
        "nix_item_id": "513fc9e73fe3ffd40300109f",
        "locale": "en_US"
       }]
    }
    

    So this should map to POJO that contains branded array, so have pojo class that consists of branded array

    public class ProductResponse{
    
     @JsonProperty("branded")
     private ProductInfo[] branded;
    
      //getters and setters
      }
    

    API call

    ResponseEntity<ProductResponse> product =  restConfig.createRestTemplate()
            .exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductResponse.class, query);
    return product;