0

Trying to read the below json and map to properties in the class. Sample code below.

     String jsonStr = "{\n" +
                    "\"requestIds\": [20123, 20124],\n" +
                    "\"resource\": {\n" +
                    "\"resourceId\": \"N\",\n" +
                    "\"debitTypeId\": \"11\",\n" +
                    "\"part\": \"NAN\",\n" +
                    "\"showAlertedTddOnly\": false,\n" +
                    "\"acknowledgement\": \"UNACKNOWLEDGED\",\n" +
                    "\"managerEngineer\": null,\n" +
                    "\"route\": [{\n" +
                    "\"rowNum\": null,\n" +
                    "\"columnNum\": \"12\"\n" +
                    "},\n" +
                    "{\n" +
                    "\"rowNum\": \"#\",\n" +
                    "\"columnNum\": \"13\",\n" +
                    "\"rowIdent\": \"SIGN\"\n" +
                    "}\n" +
                    "]\n" +
                    "}\n" +
                    "}";
 ObjectMapper om = new ObjectMapper();
 ResourceCallRequest models = om.readValue(jsonStr, ResourceCallRequest.class);

Below is the model class to which i want to map the above json.

@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResourceCallRequest {

    List<Integer> requestIds;
    String resource;

Exception :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
 at [Source: (String)"{
}

In the ResourceCallRequest class, after reading the jsonString, i want to map requestIds from json to List<Integer> requestIds and resource from jsonStr to String resource. Expected output to map to properties are as below.

List<Integer> requestIds = [20123, 20124]
String resource = "{\n" +
               "\"resourceId\": \"N\",\n" +
               "\"debitTypeId\": \"11\",\n" +
               "\"part\": \"NAN\",\n" +
               "\"showAlertedTddOnly\": false,\n" +
               "\"acknowledgement\": \"UNACKNOWLEDGED\",\n" +
               "\"managerEngineer\": null,\n" +
               "\"route\": [{\n" +
               "\"rowNum\": null,\n" +
               "\"columnNum\": \"12\"\n" +
               "},\n" +
               "{\n" +
               "\"rowNum\": \"#\",\n" +
               "\"columnNum\": \"13\",\n" +
               "\"rowIdent\": \"SIGN\"\n" +
               "}\n" +
               "]\n" +
               "}";
user7833845
  • 83
  • 2
  • 11
  • You'll have to add a custom serializer to the `resource` attribute which takes the JSON object and turns it into it's string representation. – Shadow Man Apr 05 '22 at 19:34
  • Does this answer your question? [How to get the underlying String from a JsonParser (Jackson Json)](https://stackoverflow.com/questions/16825880/how-to-get-the-underlying-string-from-a-jsonparser-jackson-json) – Shadow Man Apr 05 '22 at 19:40

0 Answers0