1

I'm trying to deserialize a JSON into an object with Jackson, but the error says

"Handling server error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token"

The JSON has this structure

[{
        "id": 685509210310,
        "intention_id": 13042,
        "cuit": "133423122",
        "branch": "Local",
        "checkout": "7",
        "establishment_id": "21312",
        "transaction_datetime": "2021-03-10T17:07:10",
        "payment_method_id": 1,
        "payment_method_code": "VI",
        "payment_method_type": "credit_card",
        "card_data": {
            "card_brand": "Visa",
            "bin": "1211111",
            "last_four_digits": "0000",
            "bank_data": {
                "id": 1,
                "description": "Banco"
            }
        },
        "amount": 1200.00,
        "currency": "ARS",
        "installments": 1,
        "status": "approved",
        "status_details": {
            "card_authorization_code": "11111",
            "card_reference_number": " 00000",
            "response": {
                "type": "approved",
                "reason": {
                    "id": 0,
                    "description": "APROBADA (authno)",
                    "ticket_footer": "INFO ADICIONAL "
                }
            }
        },
        "terminal_data": {
            "trace_number": 43,
            "ticket_number": 121,
            "terminal_number": "87212121"
        }
    }
]

I'm trying to deserialize it into my object that has lists of other classes for the JSON inside, but it doesn't work

The method to cast it is

Collection read = new ObjectMapper().readValue(new_salida, new TypeReference<Collection>() {});
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [Can not deserialize instance of java.util.ArrayList out of START\_OBJECT token](https://stackoverflow.com/questions/20837856/can-not-deserialize-instance-of-java-util-arraylist-out-of-start-object-token) – samabcde Mar 11 '21 at 13:13

3 Answers3

0

I don't think it will work the way you try it to work

This is the proper way to go

Collection<YourObject> read = new ObjectMapper().readValue(new_salida, YourObject[].class);

And YourObject must be defined so that it can hold the values of the json that you expect

>  {
>       "id":685509210310,
>       "intention_id":13042,
>       "cuit":"133423122",
>       "branch":"Local",
>       "checkout":"7",
>       "establishment_id":"21312",
>       "transaction_datetime":"2021-03-10T17:07:10",
>       "payment_method_id":1,
>       "payment_method_code":"VI",
>       "payment_method_type":"credit_card",
>       "card_data":{
>          "card_brand":"Visa",
>          "bin":"1211111",
>          "last_four_digits":"0000",
>          "bank_data":{
>             "id":1,
>             "description":"Banco"
>          }
>       },
>       "amount":1200.00,
>       "currency":"ARS",
>       "installments":1,
>       "status":"approved",
>       "status_details":{
>          "card_authorization_code":"11111",
>          "card_reference_number":" 00000",
>          "response":{
>             "type":"approved",
>             "reason":{
>                "id":0,
>                "description":"APROBADA (authno)",
>                "ticket_footer":"INFO ADICIONAL "
>             }
>          }
>       },
>       "terminal_data":{
>          "trace_number":43,
>          "ticket_number":121,
>          "terminal_number":"87212121"
>       }    }
Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
0

I found the answers with this line

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

0

You can use TypeReference for List<YourObject> and give it to the ObjectMapper.

Here is how i have done it:

class SampleCardData {
    @JsonProperty
    private Long id;
    @JsonProperty("intention_id")
    private Long intention_id;

   ...ignored other properties...

    @Override
    public String toString() {
        return "SampleCardData{" +
                "id=" + id +
                ", intention_id=" + intention_id +
                '}';
    }
}

public class SampleParseSF {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        TypeReference<List<SampleCardData>> arrayTypeRef = new TypeReference<List<SampleCardData>>() {
        };
        File file = new File("type-ref.json");
        List<SampleCardData> sampleCardDataList = objectMapper.readValue(file, arrayTypeRef);
        System.out.println(sampleCardDataList);
    }
}
silentsudo
  • 6,730
  • 6
  • 39
  • 81