How do I access nested hashmap java

I’ve a

Map<String, Object> response = {
    sendResponse = {
        return = {
            SendResponse = {
                txnID = 4fa160ce-638f-4556-9313-afbef543fadd,
                emailTypeID = 1020261131,
                txnStatusCode = 1,
                txnStatusMessage = OK,
                status = [
                    {   CommunicationStatus = {
                            type = EMAIL,
                            statusCode = 1,
                            statusMessage = OK
                        }
                    }
                ]
            }
        }
    }
}

How to extract “statusMessage” from response Map. Please advise.

The runtime types of nested objects are LinkedHashMap<K,V> and Type of the Status is ArrayList which has LinkedHashMap<K,V> inside.

I tried using get() method for example like below but I’m facing casting warnings. Ex: response.get(“sendResponse”)).get(“return”)).get(“SendResponse”).get(“status”).get(0).get(“CommunicationStatus”).get(statusMessage);

Answer

It takes a bit alright.

    Map<String, Object> response = new LinkedHashMap<>(Map.of(
            "sendResponse", new LinkedHashMap<>(Map.of(
                    "return", new LinkedHashMap<>(Map.of(
                            "SendResponse", new LinkedHashMap<>(Map.of(
                                    "txnId", "4fa160ce-638f-4556-9313-afbef543fadd",
                                    "status", new ArrayList<Object>(List.of(
                                            new LinkedHashMap<>(Map.of(
                                                    "CommunicationStatus", new LinkedHashMap<String, Object>(Map.of(
                                                            "type", "EMAIL",
                                                            "statusMessage", "OK"))))))))))))));
    
    System.out.println(response);
    
    Map<?, ?> sendResponse = (Map<?, ?>) response.get("sendResponse");
    Map<?, ?> returnMap = (Map<?, ?>) sendResponse.get("return");
    Map<?, ?> sendResponseCapitalS = (Map<?, ?>) returnMap.get("SendResponse");
    List<?> status = (List<?>) sendResponseCapitalS.get("status");
    Optional<Map<?, ?>> statusEmail = (Optional<Map<?, ?>>) status.stream()
            .map(m -> ((Map<?, ?>) m).get("CommunicationStatus"))
            .filter(m -> ((Map<?, ?>) m).get("type").equals("EMAIL"))
            .findAny();
    Optional<Object> statusMessage = statusEmail.map(m -> m.get("statusMessage"));
    
    statusMessage.ifPresent(System.out::println);

I have left out some uninteresting bits from your map, it should make no difference. I am printing the map I have constructed for you to check that it looks like yours (only with a few bits left out). Output from the above snippet is:

{sendResponse={return={SendResponse={txnId=4fa160ce-638f-4556-9313-afbef543fadd,status=[{CommunicationStatus={statusMessage=OK, type=EMAIL}}]}}}}
OK

I am using a stream operation to take out the map from the list where type is EMAIL. Please check whether this is correct for your requirements.

Edit: You and I have both observed the warning Type safety: Unchecked cast from Optional<capture#16-of ?> to Optional<Map>. I found no good way of getting rid of it. The following variant of the last two lines before the final printout do not show the warning:

    Optional<?> statusEmail = status.stream()
            .map(m -> ((Map<?, ?>) m).get("CommunicationStatus"))
            .filter(m -> ((Map<?, ?>) m).get("type").equals("EMAIL"))
            .findAny();
    Optional<Object> statusMessage = statusEmail.map(m -> ((Map<?, ?>) m).get("statusMessage"));

However, they give less information about the type of statusEmail, which I find a clear disadvantage, which is why I opted for posting the first variant above in spite of the warning.

Leave a Reply

Your email address will not be published. Required fields are marked *