javaspringstringobjectmapperresponse-entity

error attempting to convert the JSON string response from the API to a Map<String, Object>


I am encountering an issue while consuming an API that returns a JSON string. When attempting to convert the JSON string response from the API to a Map<String, Object>, it throws an error. However, when I manually embed the same JSON string response into my code and perform the conversion to a Map<String, Object>, it works seamlessly. Understanding this behavior has proven to be quite challenging for me


Error :

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testService': Invocation of init method failed; nested exception is java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.LinkedHashMap (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{') at [Source: (String)""{"DXX":{"NX":"XXDHX XXK XIMIXED","XXXX":"X001654011000000087","NXme1":"XINHX","XXhmId":"16401","XXhmNXme":"MXXXEXXXXD WXXXD","InXXXX":"1","XXXNXme":"XXXIVE","XXXXhgDX":"00000000","XpeXXXXMXinX":"XYX","DXXXXMXinX":"00000000","XvXXXg":"I","XvXXXgNXme":"INDIVIDUXX","XuXXDiXXXXg":"N","XuXXDiXXXXXDX":"00000000","XuXXDiXXXXXX":"0000000000000000000000000","XXkMXde":"P","DeXqMXde":"P","HighXedeemXmX":"0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXnd":"200","XXdPXXXXughX":"0","XXdPXXXnuX":"0","[truncated 1399 chars]; line: 1, column: 1]


Json String :

{
  "DXX": {
    "NX": "XXDHX XXK XIMIXED",
    "XXXX": "X001654011000000087",
    "NXme1": "XINHX",
    "XXhmId": "16401",
    "XXhmNXme": "MXXXEXXXXD WXXXD",
    "InXXXX": "1",
    "XXXNXme": "XXXIVE",
    "XXXXhgDX": "00000000",
    "XpeXXXXMXinX": "XYX",
    "DXXXXMXinX": "00000000",
    "XvXXXg": "I",
    "XvXXXgNXme": "INDIVIDUXX",
    "XuXXDiXXXXg": "N",
    "XuXXDiXXXXXDX": "00000000",
    "XuXXDiXXXXXX": "0000000000000000000000000",
    "XXkMXde": "P",
    "DeXqMXde": "P",
    "HighXedeemXmX": "0",
    "HighXedeemPXX": "0",
    "XXXXXhVXX": "0",
    "XXdPXXEXXnd": "70610",
    "XXdPXXXXughX": "0",
    "XXdPXXXnuX": "0",
    "XXdPXXXedeemed": "967",
    "XXdPXXXXXed": "0",
    "XXdPXXExpiXed": "0",
    "XXdPXXXdjIn": "10000",
    "XXdPXXXdjXuX": "0",
    "GXXXeXXd": "0",
    "XyXXXDXXXX": "10198",
    "XXdXpndEXXnd": "0",
    "YXdXpendEXXnd": "0",
    "XiXeXXDXXXX": "79643",
    "PXPXXXNXXXXd": "0",
    "XpenXXXedXnXXX": "79643",
    "XXmXMXhXXeq": "1",
    "XXmXDXyXXMXh": "15",
    "XXXXXmXDX": "20231215",
    "NexXXXmXDX": "20240115",
    "DXXpened": "20231204",
    "DiXXPkXPXX": "0",
    "DiXXPkXMXnVXXPXX": "0",
    "XuXXDiXXXXeq": "0",
    "XuXXDiXXDXyXXMXh": "0",
    "XXXXDiXXuXXeDX": "00000000",
    "XuXXDiXXXhXeXhXd": "0",
    "XuXXDiXXMXnVXX": "0",
    "WXiveMemXXee": "N",
    "XXXpnDX": "2023338",
    "XeXXXeXmX": "0",
    "XuXXNXd": "2",
    "PeXIXemNXd": "0",
    "PXXXXXeNXd": "7",
    "XnXXyXd": "IND",
    "XuXXXd": "356",
    "NXXMXhDXXXXXuX": "1",
    "MXnXhXyXXXhXXXkGXXup": [
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "20231215"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      }
    ],
    "XXXXXpenXniDXXe": "20241204",
    "XXdDiXXXXXhXXXk": "0",
    "XXdXpendXmX": "0"
  }
}

code:

        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.ALL));
        headers.add("api_key","qwerqwersadfaf");
        headers.add("x-session-id","asdfasdf");
        headers.add("x-user-id","asdfasdfas");
        headers.add("x-message-id","asdfasdfas");

        String bodyTopost = "{\"Data\": {\"Account\": \"SAMPLEaccount\",\"Schm\": \"168401\"}}";

        HttpEntity<String> entity = new HttpEntity<String>(bodyTopost, headers);
        ParameterizedTypeReference<String> parameterizedTypeReference = new ParameterizedTypeReference<String>() {
        };
        String inputUrl = "https://api.testbank.co.in/cons/abcd/v1";

        try {
            ResponseEntity<String> result = restTemplate.exchange(inputUrl, HttpMethod.POST, entity, parameterizedTypeReference);
            if (result.getStatusCode().is2xxSuccessful()) {
                if (result.getBody() != null) {

                     String resultBody = result.getBody().replace("\\", "");

                    logger.info("Response value is {}", resultBody);

                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map =  mapper.readValue(resultBody, Map.class);

                    return map;
                } else {
                    logger.info("Payload is null");
                    return null;
                }
            } else {
                logger.info("Failed to execute api");
                return null;
            }
        } catch (ResourceAccessException e) {
            logger.info("Resource Erro  {} ", e);
            return null;
        } catch (RestClientException e) {
            logger.info("Rest Error Erro  {} ", e);
            return null;

        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

When attempting to convert the JSON string response from the API to a Map<String, Object>, it throws an error. However, when I manually embed the same JSON string response into my code and perform the conversion to a Map<String, Object>, it works seamlessly.

String data="{"DXX":{"NX":"XXDHX XXK XIMIXED","XXXX":"X001654011000000087","NXme1":"XINHX","XXhmId":"16401","XXhmNXme":"MXXXEXXXXD WXXXD","InXXXX":"1","XXXNXme":"XXXIVE","XXXXhgDX":"00000000","XpeXXXXMXinX":"XYX","DXXXXMXinX":"00000000","XvXXXg":"I","XvXXXgNXme":"INDIVIDUXX","XuXXDiXXXXg":"N","XuXXDiXXXXXDX":"00000000","XuXXDiXXXXXX":"0000000000000000000000000","XXkMXde":"P","DeXqMXde":"P","HighXedeemXmX":"0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXnd":"200","XXdPXXXXughX":"0","XXdPXXXnuX":"0","XXdPXXXedeemed":"2","XXdPXXXXXed":"0","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","GXXXeXXd":"0","XyXXXDXXXX":"10198","XXdXpndEXXnd":"0","XXdPXXEXXnd":"70610","XXdPXXXXughX":"0","XXdPXXXnuX":"0","XXdPXXXedeemed":"967","XXdPXXXXXed":"0","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","YXdXpendEXXnd":"0","GXXXeXXd":"0","XiXeXXDXXXX":"79643","PXPXXXNXXXXd":"0","XpenXXXedXnXXX":"79643","XXmXMXhXXeq":"1","XXmXDXyXXMXh":"15","XXXXXmXDX":"20231215","NexXXXmXDX":"20240115","DXXpened":"20231204","DiXXPkXPXX":"0","DiXXPkXMXnVXXPXX":"0","XuXXDiXXXXeq":"0","XuXXDiXXDXyXXMXh":"0","XXXXDiXXuXXeDX":"00000000","XuXXDiXXXhXeXhXd":"0","XuXXDiXXMXnVXX":"0","WXiveMemXXee":"N","XXXpnDX":"2023338","XeXXXeXmX":"0","XuXXNXd":"2","PeXIXemNXd":"0","PXXXXXeNXd":"7","XnXXyXd":"IND","XuXXXd":"356","NXXMXhDXXXXXuX":"1","MXnXhXyXXXhXXXkGXXup":[{"DiXXXmX":"0","DiXXMXhDX":"20231215"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"}],"XXXXXpenXniDXXe":"20241204","XXdDiXXXXXhXXXk":"0","XXdXpendXmX":"0"}}";

ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map = mapper.readValue(data, Map.class);

The error occurs when I directly convert the response from the API to Map<String, Object>


Solution

  • It seams the string returned from API has two extra quotes, one of begin and other at the end of string, look the source property at stack trace:[Source: (String)""{"DXX":{"NX":"XXDHX XXK ...

    An option to resolve this is to remove the extra quotes before to read the values, like this:

    int begin = resultBody.indexOf("{");
    int end = resultBody.lastIndexOf("}") + 1;
    resultBody= resultBody.substring(begin, end);
    

    Code used to reproduce this error:

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.util.Map;
    
    public class Main {
    
      public static void main(String[] args) throws JsonProcessingException {
    
        String resultBody="\"{\"DXX\":{\"NX\":\"XXDHX XXK XIMIXED\",\"XXXX\":\"X001654011000000087\",\"NXme1\":\"XINHX\",\"XXhmId\":\"16401\",\"XXhmNXme\":\"MXXXEXXXXD WXXXD\",\"InXXXX\":\"1\",\"XXXNXme\":\"XXXIVE\",\"XXXXhgDX\":\"00000000\",\"XpeXXXXMXinX\":\"XYX\",\"DXXXXMXinX\":\"00000000\",\"XvXXXg\":\"I\",\"XvXXXgNXme\":\"INDIVIDUXX\",\"XuXXDiXXXXg\":\"N\",\"XuXXDiXXXXXDX\":\"00000000\",\"XuXXDiXXXXXX\":\"0000000000000000000000000\",\"XXkMXde\":\"P\",\"DeXqMXde\":\"P\",\"HighXedeemXmX\":\"0\",\"HighXedeemPXX\":\"0\",\"XXXXXhVXX\":\"0\",\"XXdPXXEXXnd\":\"200\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"2\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"GXXXeXXd\":\"0\",\"XyXXXDXXXX\":\"10198\",\"XXdXpndEXXnd\":\"0\",\"XXdPXXEXXnd\":\"70610\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"967\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"YXdXpendEXXnd\":\"0\",\"GXXXeXXd\":\"0\",\"XiXeXXDXXXX\":\"79643\",\"PXPXXXNXXXXd\":\"0\",\"XpenXXXedXnXXX\":\"79643\",\"XXmXMXhXXeq\":\"1\",\"XXmXDXyXXMXh\":\"15\",\"XXXXXmXDX\":\"20231215\",\"NexXXXmXDX\":\"20240115\",\"DXXpened\":\"20231204\",\"DiXXPkXPXX\":\"0\",\"DiXXPkXMXnVXXPXX\":\"0\",\"XuXXDiXXXXeq\":\"0\",\"XuXXDiXXDXyXXMXh\":\"0\",\"XXXXDiXXuXXeDX\":\"00000000\",\"XuXXDiXXXhXeXhXd\":\"0\",\"XuXXDiXXMXnVXX\":\"0\",\"WXiveMemXXee\":\"N\",\"XXXpnDX\":\"2023338\",\"XeXXXeXmX\":\"0\",\"XuXXNXd\":\"2\",\"PeXIXemNXd\":\"0\",\"PXXXXXeNXd\":\"7\",\"XnXXyXd\":\"IND\",\"XuXXXd\":\"356\",\"NXXMXhDXXXXXuX\":\"1\",\"MXnXhXyXXXhXXXkGXXup\":[{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"20231215\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"}],\"XXXXXpenXniDXXe\":\"20241204\",\"XXdDiXXXXXhXXXk\":\"0\",\"XXdXpendXmX\":\"0\"}}\"";
    //    int begin = resultBody.indexOf("{");
    //    int end = resultBody.lastIndexOf("}") + 1;
    //    resultBody= resultBody.substring(begin, end);
        Map<String, Object> map = new ObjectMapper().readValue(resultBody, Map.class);
        System.out.println(map.entrySet());
      }
    }