'How to deserialize value of type from array value?

I'm getting an error when trying to convert JSON to an object and I have no idea how I can solve this.

I am trying to map such JSON:

{
    "code": "USD",
    "rates": [
        {
            "effectiveDate": "2022-04-12",
            "mid": 4.2926
        }
    ]
}


Solution 1:[1]

rates property is array in json, rates field is object in your class. You need to deserialize in the same type - array to array/list, object to object, etc. Make the field array or list in class and you are good to go.

@Getter
public class CurrencyDto {
    private String code;
    private CurrencyRatesDto[] rates;
}

Solution 2:[2]

The error Cannot deserialize value of type com.example.nbpmaster.webclient.dto.CurrencyRatesDto from Array value (token JsonToken.START_ARRAY is clear.

The deserializer is expecting rates to be an Object, but it found a JsonToken.START_ARRAY, which is the char [.

You are trying to deserialize a JSON array into a Object (CurrencyRatesDto).

In your CurrencyDto class, change the CurrencyRatesDto to some sort of Collection or primitive array -- I used List<CurrencyRatesDto> in the following example

@Getter
public class CurrencyDto {
    private String code;
    private List<CurrencyRatesDto> rates;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chaosfire
Solution 2 Matheus Cirillo