Error parsing JSON (MismatchedInputException)

Tags: , ,



I’m having problems parsing JSON, this is the error:

out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<packagename....>` out of START_OBJECT token

And I know why it is happening I just don’t know how to fix it. This JSON works:

{
    "status_code": "SUCCESS",
    "time": {
        "date": "Mar 23, 2021 1:14:39 AM"
    },
    "info": [
        {
            "person": "2.2",
            "role": "TEACHER"
        },
        {
            "person": "2.3",
            "role": "TEACHER"
        }
    ]
}

This one does not:

{
    "status_code": "SUCCESS",
    "time": {
        "date": "Mar 23, 2021 3:49:27 AM"
    },
    "info": {
        "id": "1",
        "person": [
            {
                "identifier": "John",
                "role": "TEACHER"
            },
            {
                "identifier": "Homer",
                "role": "TEACHER"
            },
            {
                "identifier": "Michael",
                "role": "TEACHER"
            },
            {
                "identifier": "Sarah",
                "role": "TEACHER"
            }
        ]
    }
}

The problem seems to be the { character in front of the info field because with [ works. So this is the method I’m using to parse the JSON:

public Mono<PersonResponse> searchById(String id) {
        return webClient.get().uri(id).retrieve().bodyToMono(PersonResponse.class);
}

Also tried:

public Mono<PersonResponse[]> searchById(String id) {
            return webClient.get().uri(id).retrieve().bodyToMono(PersonResponse[].class);
}

Error right on line: 1, column: 1. Any suggestions of how to implement the method?

EDIT: Added classes.

PersonResponse:

public class PersonResponse implements Serializable{
        
    private static final long serialVersionUID = 7506229887182440471L;
        
    public String status_code;
    public Timestamp time;  
    public List<PersonDetails> info;

    public PersonResponse() {}

    ...getters / setters / toSting

PersonDetails:

public class PersonDetails implements Serializable{

private static final long serialVersionUID = 1294417456651475410L;

private int id;
private List<Person> person;

public PersonDetails(int version) {
    super();
    this.version = version;
}

...getters / setters / toSting

Person

public class Person implements Serializable{
    
    private static final long serialVersionUID = 3290753964441709903L;
    
    private String identifier;
    private String role;
    
    public Person(String identifier, String role) {
        super();
        this.identifier = identifier;
        this.role = role;
    }

    ...getters / setters / toSting

Answer

The problem isn’t the JSON necessarily, it’s that the JSON structure doesn’t match your PersonResponse class. There’s an info variable in PersonResponse that requires an array of what I assume to be persons, in the second example you’re trying to push an object in there, which you can’t. You have to either change your JSON, which you don’t seem to want in this case, or the class you’re trying to parse it to.

You need to restructure the info variable in PersonResponse to match the object you’re trying to parse to it.



Source: stackoverflow