-1

I have a response Body

[{"vendorName":"ABC","vendorAPI":"Some_API","enrichmentGroupingLevel":[{"Test":{"score":0.233,"risckscore":229},"level":"all"}],"error":{},"statusCode":200}]",
CoreV2Response[] getEnrichmentResponse = objectMapper.readValue(responseBody, CoreV2Response[].class);

The below is my coreV2Response class which i want to map with

public class CoreV2Response {
            @JsonProperty("vendorName")
            private String vendorName;
            @JsonProperty("vendorAPI")
            private String vendorAPI;
            @JsonProperty("enrichmentGroupingLevel")
            private Object[] enrichmentGroupingLevel;
            @JsonProperty("error")
            private Object[] error;
            @JsonProperty("statusCode")
            private int statusCode;
        }
I am getting error 
Cannot deserialize instance of [Ljava.lang.Object; out of START_OBJECT tokenat [Source: (String)"[{"vendorName":"Ekata","vendorAPI":"Account_Opening_V1.0","enrichmentGroupingLevel
```
 
Can someone help me with deserialization?
I have added my both the codes for response template class and mapper object
theBeginner
  • 85
  • 10

1 Answers1

1

In your json string error is in object

"error": {}

But in CoreV2Response you have declared the error field as object array:

private Object[] error;

You cannot deserialize json object into collection, array in your case. You have to declare error as an object

private Object error;

And deserialization goes without problems.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • Can i access each variable in the class by getEnrichmentResponse.getvendorName like this? – theBeginner Jan 24 '22 at 06:32
  • @krishnagiri2000 I'm not sure i understand the question. If you are asking how to access data in error, since the field is declared as `Object` it is deserialized as a `Map`, so you need to cast in to a Map and use methods provided by map to access data in it. – Chaosfire Jan 24 '22 at 07:03
  • Is i am calling the method of object mapper class correctly , considering the response body is an string which is an array of objects? – theBeginner Jan 24 '22 at 09:46
  • 1
    @krishnagiri2000 Yes, calling object mapper like this - `CoreV2Response[] getEnrichmentResponse = objectMapper.readValue(responseBody, CoreV2Response[].class);` is absolutely correct in your case. The only error was what i wrote in the answear, everything else is correct. – Chaosfire Jan 24 '22 at 10:05
  • like in my case the enrichmentGroupingLevel is array of objects, so I can cast this to a list of object right so it will be like - List newParams = new ArrayList(Arrays.asList(getEnrichmentResponse[0].getEnrichmentGroupingLevel())); FYI-getEnrichmentGroupingLevel is my getter methof for getEnrichmentResponse – theBeginner Jan 25 '22 at 06:32
  • 1
    @krishnagiri2000 Yes, thats one of the ways to do it, and it is a correct one. – Chaosfire Jan 25 '22 at 07:00
  • Now in the above case i will get enrichmentGroupingLevel by getEnrichmentReponse[0].getEnrichmentGroupingLevel() which will be array of objects ...Now It is an array containing objects so i want to fetch the first object out of it , how can i do it? – theBeginner Jan 27 '22 at 07:29
  • @krishnagiri2000 [how to get first/last element in array](https://stackoverflow.com/questions/39860739/how-to-get-first-and-last-element-in-an-array-in-java/39860854) – Chaosfire Jan 27 '22 at 07:37
  • Is there any way to manipulate with object in java or i have to convert it to hashmap and then use hashmap methodds? Lets say I have to replace a value for particular key then it would be simple in hashmap but in object we cannot do it – theBeginner Jan 31 '22 at 06:44
  • @krishnagiri2000 That should be a separate question altogether. – Chaosfire Jan 31 '22 at 07:30