简体   繁体   中英

How do I merge two json files having different types?

In the code mentioned below,

String additionalAttributes = new ObjectMapper().writeValueAsString(retrieveAttributes(requestApplication));

"additionalAttributes" is giving me first json response and sessionValidationResponse is giving me another json response, please note that both are having different return types(String and SessionValidationResponse ) how do I merge these responses to send a single json response

public SessionValidationResponse checkToken(String tokenId) throws RuntimeException {
    System.out.println(" \n \n \n Enter in checkToken method");
    System.out.println("Received Token ID: " + tokenId);


    if (null != tokenId && tokenId != "") {
        try {


            String filepath = "session_validation.json";
            String requestApplication = "myVf";
            String additionalAttributes = new ObjectMapper().writeValueAsString(retrieveAttributes(requestApplication));
            System.out.println("additionalAttributes: " +additionalAttributes);


            try {
                SessionValidationResponse sessionValidationResponse = jsonFileReader.read(filepath, new TypeReference<SessionValidationResponse>() {
                });
                sessionValidationResponse.setToken(createToken(sessionValidationResponse.getUserName(), new Date(), Calendar.getInstance().getTime()));
                return sessionValidationResponse;
            } catch (Exception e) {
                throw new RuntimeException();
            }


        } catch (SignatureException e) {
            throw new InvalidTokenException(new Exception(INVALID_TOKEN));
        } catch (ExpiredJwtException e) {
            throw new TokenExpirationException(new Exception(TOKEN_EXPIRED));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    } else {
        throw new TokenEmptyException(new Exception(TOKEN_NOT_FOUND));
    }
    return null;
}

Response of String type: {"status":"ACTIVE","msisdn":"45695","UT_MOBILE":"4258963144","gender":"MALE"}

Response of SessionValidationResponse type

{
  "valid": true,
  "uid": "demo",
  "userName": "ABC",
  "token": "randomToken",
  "msisdn": "9949999499"
}

Found the solution. I casted both the values into a HashMap and merged them into a single (new)Map using putAll(map1, map2); using Map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM