Search code examples
javajsonserializationdeserializationmodelmapper

How to fix Error START_ARRAY token when deserializing Json with ModelMapper?


I'm trying to deserialize a JSON file in this format:

[
  ["AA", "GG", "1992/11/18"],
  ["BB", "DD", "2005/02/20"]
]

Using this class:

public class DataList {
    private List<String> att;
    // constructor, getter and setter
}

doing:

DataList [] dataList= mapper.readValue(ResourceUtils.getFile("classpath:" + filename), DataList [].class);

But I'm getting:

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.example.DataList ` out of START_ARRAY token
 at [Source: (File); line: 2, column: 3] (through reference chain: java.lang.Object[][0])

Any idea on how to fix this error?


Solution

  • Jackson doesn't know how to map array of strings to DataList object. So you should just add @JsonCreate on DataList constructor to show Jackson what to use for the conversion.

    public class DataList {
    
        private List<String> att;
    
        @JsonCreator
        public DataList(List<String> att) {
            this.att = att;
        }
    
        // constructor, getter and setter
    }