0

I'm trying to map JSON to ArrayList and I'm getting "Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token" error and I don't understand why. I found this article: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token resolving similar problem (there was invalid JSON), but it seems like I have valid json file. I'm using maping method presented in this question: How to use Jackson to deserialise an array of objects

I found this article: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token resolving similar problem (there was invalid JSON), but it seems like I have valid json file. I'm using maping method presented in this question: How to use Jackson to deserialise an array of objects

My Json looks like this:

[
    { 
        "id": "12345", 
        "name": "John" 
    }, 
    { 
        "id": "09876", 
        "name": "Desmond" 
    }
]

Data model:

public class Student {

    private int id;
    private String name;

    public Student() {}

    // getters, setters and tostring
}

Parsing code:

ObjectMapper objectMapper = new ObjectMapper();

Path pathToStudentsJson = Paths.get("src/main/resources/static/students.json");
File studentsFile = new File(pathToSingleStudentJson.toString());

List<Student> listOfStudents = objectMapper.readValue(studentsFile, new TypeReference<List<Student>>(){});

The error, that I'm getting:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
 at [Source: (File); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2940)
    at io.json.JsonApp.main(JsonApp.java:46)

Process finished with exit code 1
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
s-kaczmarek
  • 161
  • 1
  • 13

2 Answers2

0

Your Data model shows Id is int whereas in the JSON it is string. Try changing this and use latest version of Jackson. It may resolve the issue.

Niran
  • 21
  • 1
  • This is not the case, because I was trying with single student JSON file and id was mapped properly to int inside Student object. Anyway I can fix that to keep data types consistent, but this is not solution for my problem. – s-kaczmarek May 12 '19 at 07:03
0

All right, I have found my mistake :) It's here:

Path pathToStudentsJson = Paths.get("src/main/resources/static/students.json"); 
File studentsFile = new File(pathToSingleStudentJson.toString());

I'm creating File object referring to the incorrect path- I should pass pathToStudentsJson to the constructor instead of pathToSingleStudentJson

s-kaczmarek
  • 161
  • 1
  • 13