Tuesday, 18 July 2017

Brute Force Removal of Json Attribute from Json String : IBM JSON JAVA LIB

       


        
  // Invocation Sending a json String and removal of key elements from the json String
    String logString = json;
    logString = maskedLog(logString);
    logger.info("Response Data : "+logString);
    
    
    
  public static String maskedLog(String logString){
 
    for (String s : sensitiveKeys){
     try {
      logString=   removeField(s, logString);
    } catch (IOException e) {
     logger.severe("Exception in Util maskedLog ");
    }
    }
    return logString;
 }  
    
   public static String removeField(String key, String jsonString) throws NullPointerException, IOException {
  JSONArtifact artifact = (JSONArtifact) JSON.parse(jsonString);     
  removeField(key, artifact);      
  return artifact.toString();  
 }

 private static void removeField(String key, Object json) {
  if (json instanceof JSONObject) {
   JSONObject o = (JSONObject) json;
   o.remove(key);
   Collection values = o.values();
   traverseCollection(key, values);
  } else if (json instanceof JSONArray) {
   JSONArray array = (JSONArray) json;
   traverseCollection(key, array);
  }
 }

 private static void traverseCollection(String key, Collection collection) {
  for (Object o : collection) {
   removeField(key, o);
  }
 }
 
 

       
 

No comments:

Post a Comment

AWS Certificate Manager - Import Certificate

How to import a certificate. Use Key Store Explorer to open the cert and export keypair in PEM format  This will save a file cert.pem. Make ...