Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

robbie70 :

I am getting the below error message, can someone please help or suggest how best to debug this.

Cannot deserialize instance of java.lang.String out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 37610] (through reference chain: com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price["now"])

I am trying to deserialise a Products object from a REst API call. The code has been working fine until I added code to deserialise a Price sub-class. This looks as follows,

"price": {
        "was": "",
        "then1": "",
        "then2": "",
        "now": "59.00",
        "uom": "",
        "currency": "GBP"
      },

My Price pojo looks as follows,

public class Price {

     @JsonProperty("was")
     String was;
     @JsonProperty("then1")
     String then1;
     @JsonProperty("then2")
     String then2;
     @JsonProperty("now")
     String now;
     @JsonProperty("uom")
     String uom;
     @JsonProperty("currency")
     String currency;

     public Price() {
         //blank constructor for JSON
     }

     @Override
     public String toString() {
         return "Price{" +
                 "was='" + was + '\'' +
                 ", then1='" + then1 + '\'' +
                 ", then2='" + then2 + '\'' +
                 ", now='" + now + '\'' +
                 ", uom='" + uom + '\'' +
                 ", currency='" + currency + '\'' +
                 '}';
     }
}

I have written a Junit test to try and simulate the error but it works in my test,

    @Test
    public void shouldConvertJsonProductListIntoPrice() {
        ObjectMapper objectMapper = new ObjectMapper();
        String content3 = "{\"products\": [{\"productId\": \"3525085\",\"title\": \"hush Tasha Vest Dress\", " +
                "\"price\": {\"was\": \"\",\"then1\": \"\",\"then2\": \"\",\"now\": \"59.00\",\"uom\": \"\",\"currency\": \"GBP\"}, " +
                "\"colorSwatches\": [{\"basicColor\": \"Red\",\"skuId\": \"237494589\"},{\"basicColor\": \"Blue\",\"skuId\": \"237494562\"}] " +
                "}]}";
        JavaType valueType = objectMapper.constructType(ProductList.class);
        ProductList readValue;
        try {
            readValue = objectMapper.readValue(content3, valueType);
            assertEquals("3525085", readValue.getProductList().get(0).productId);
            assertEquals("hush Tasha Vest Dress", readValue.getProductList().get(0).title);
            assertEquals("", readValue.getProductList().get(0).price.then1);
            assertEquals("59.00", readValue.getProductList().get(0).price.now);
            assertEquals("Blue", readValue.getProductList().get(0).colorSwatches[1].basicColor);
            assertEquals("237494562", readValue.getProductList().get(0).colorSwatches[1].skuId);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

If I comment out the "now" field then my RestAPI call works perfectly and I dont see the exception. So it seems that there is a problem with the "now" field and here I notice that its trying to convert "59.00" into a String. Could this be a problem for the Fasterxml converter ? do I need to help it maybe ?

The Product class is as follows (although this is much reduced list of fields that I am receiving off the API call).

public class Product {

     @JsonProperty("productId")
     String productId;

     @JsonProperty("title")
     String title;

     @JsonProperty("colorSwatches")
     ColorSwatch [] colorSwatches;

     @JsonProperty("price")
     Price price;

     public Product(){
         // blank required for Jackson
     }

     public Product(String productId, String title, ColorSwatch[] colorSwatches, Price price){
         this.productId = productId;
         this.title = title;
         this.colorSwatches = colorSwatches;
         this.price = price;
  }
Loïc Le Doyen :

The error states it expects a VALUE (VALUE_STRING preferably), whereas it gets a START_OBJECT, so your issue is probably coming from a json of the form

"price": {
    "was": "",
    "then1": "",
    "then2": "",
    "now":  {
        ...
    }
    "uom": "",
    "currency": "GBP"
},

Instead of the "now": "some value" form expected by the code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of START_OBJECT token

Trouble with understanding exception: "Cannot deserialize instance of `java.lang.String` out of START_OBJECT token" using ObjectMapper in Jackson

Cannot deserialize instance of `java.lang.Boolean` out of START_OBJECT token

JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token 1

Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token

Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

Cannot deserialize instance of int[] out of START_OBJECT token

Spring: Cannot deserialize instance of ENTITY out of START_OBJECT token

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

Can not deserialize instance of java.lang.Class out of START_OBJECT token

jackson kotlin - Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

onRetrofitFailure() Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

How to Fix This ERROR "JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT

Why “Cannot deserialize instance of `` out of START_OBJECT token” throws when using Jackson

Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token

Jakson can not deserialize instance of java.util.ArrayList out of START_OBJECT token

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.utils.POJO[]` out of START_OBJECT token

jsonMappingException org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

Cannot deserialize instance of <Object> out of START_ARRAY token - webClient

Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

Can not deserialize instance of java.lang.Boolean out of START_ARRAY token

Cannot deserialize instance of `Books` out of START_ARRAY token

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

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive