-1

Issue while parsing JSON into DTO.

JSON response is :

{
  "content": [
    {
      "id": 350,
      "reg": "FA-2001",
      "Email": "abc@gmail.in",
      "Mobile": "+9192000000",
      "Name": "dr kumar",
      "Ip": "0:0:0:0:0:0:0:1",
      "Datetime": "2022-10-20T13:50:49",
    }
  ],
  "pageable": {
    "sort": {
      "unsorted": false,
      "sorted": true,
      "empty": false
    },
    "pageNumber": 0,
    "pageSize": 20,
    "offset": 0,
    "paged": true,
    "unpaged": false
  },
  "last": true,
  "totalPages": 1,
  "totalElements": 1,
  "first": true,
  "sort": {
    "unsorted": false,
    "sorted": true,
    "empty": false
  },
  "numberOfElements": 1,
  "size": 20,
  "number": 0,
  "empty": false
}

DTO

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private Map content;
}

Getting

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.LinkedHashMap<java.lang.Object,java.lang.Object> from Array value (token JsonToken.START_ARRAY)

Please suggest how to get value in Map or some other object My main focus is to send specific key value pair in response.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
vegeta
  • 25
  • 1
  • 9
  • 1
    "Getting json parsing error" - Show us the error message / stacktrace – Stephen C Apr 12 '23 at 04:45
  • not parsing but de serialize error com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.LinkedHashMap` from Array value (token `JsonToken.START_ARRAY`) – vegeta Apr 12 '23 at 04:58
  • 1
    Please [edit] your question to include the full stack trace. – Code-Apprentice Apr 12 '23 at 05:25

1 Answers1

1

In your json String, the content is a List. what you can do is to update the OtherResponse accordingly:

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private List<Map<String, String>> content;

    private Pageable pageable;
}

Or even define a dedicated class for it:

@Data
public class OtherResponse {
    @JsonProperty(value = "content")
    private List<Person> content;

    private Pageable pageable;
}

PS: updated answer and added the Pageable field. I am using here the Pageable interface imported from org.springframework.data.domain.Pageable

Emanuel Trandafir
  • 1,526
  • 1
  • 5
  • 13
  • thank you @Emanuel it's working for me i am getting { "content": [ { "id": "350", "registrationNo": "FQ-123", "Email": "test1@gmail.in" } ] } i just need to remove map name from the json output and want like this { "data": [ { "id": "350", "registrationNo": "FQ-123", "Email": "test1@gmail.in" } ] } – vegeta Apr 12 '23 at 06:32
  • i have created dedicated class to parse the json data, Now i need one more help i want to save **pageable** key into DTO dedicated class my json is `{ "data": [ { "id": 350, "registrationNo": "FA-01", "applicantEmail": "test@nn.kk", "applicantMobile": "9999999998", } ], "pageable": { "sort": { "unsorted": false, "sorted": true, "empty": false }, "pageNumber": 0, "pageSize": 20, "offset": 0, "paged": true, "unpaged": false } }` – vegeta Apr 12 '23 at 08:05
  • i have updated the initial answer, take a look :) – Emanuel Trandafir Apr 12 '23 at 08:08
  • getting exception 'com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Pageable (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information` – vegeta Apr 12 '23 at 09:10
  • you can try to fix this as described here: https://stackoverflow.com/questions/52490399/spring-boot-page-deserialization-pageimpl-no-constructor or you can create your own pageable implementation – Emanuel Trandafir Apr 12 '23 at 09:45