@SuppressWarnings("unchecked") @Test public void flatNestedMap() throws Exception { Gson gson = new Gson(); String jsonObject = "{\"ruleUUID\":\"qqqq123\",\"hits\":{\"total\":1000,\"max_score\":null,\"hits\":[{\"_index\":\"bank\",\"_type\":\"_doc\",\"_id\":\"0\",\"sort\":[0],\"_score\":null,\"_source\":{\"account_number\":0,\"balance\":16623,\"firstname\":\"Bradshaw\",\"lastname\":\"qwe\",\"age\":29,\"gender\":\"F\",\"address\":\"2133\",\"employer\":\"123\",\"email\":\"bradshawqwe@123.com\",\"city\":\"Hobucken\",\"state\":\"CO\"}}]},\"aggregations\":{\"avg-values-per-day\":{\"buckets\":[{\"key_as_string\":\"ID\",\"Avg-CPU-Utilization\":{\"value\":12},\"Avg-NetworkIn\":{\"value\":12},\"Avg-NetworkOut\":{\"value\":12},\"Avg-DiskReadinBytes\":{\"value\":12},\"Avg-DiskWriteinBytes\":{\"value\":12}}]}}}"; Map<String, Object> json = (Map<String, Object>) gson.fromJson(jsonObject, Object.class); Map<String, String> response = CommonUtils.flatNestedMap(".", json); assertNotNull(response); }
@SuppressWarnings("unchecked") public static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap) { Map<String, String> flatNestedMap = new HashMap<String, String>(); String prefixKey = notation != null ? notation + "." : ""; for (Map.Entry<String, Object> entry : nestedMap.entrySet()) { if (entry.getValue() instanceof String) { flatNestedMap.put(prefixKey + entry.getKey(), (String) entry.getValue()); } if (entry.getValue() instanceof Long || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean || entry.getValue() instanceof Float) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(entry.getValue())); } if (entry.getValue() instanceof Double) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(new BigDecimal(String.valueOf(entry.getValue())).longValue())); } if (entry.getValue() instanceof Map) { flatNestedMap.putAll(flatNestedMap(prefixKey + entry.getKey(), (Map<String, Object>) entry.getValue())); } } return flatNestedMap; }
CommonUtils { @SuppressWarnings("unchecked") public static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap) { Map<String, String> flatNestedMap = new HashMap<String, String>(); String prefixKey = notation != null ? notation + "." : ""; for (Map.Entry<String, Object> entry : nestedMap.entrySet()) { if (entry.getValue() instanceof String) { flatNestedMap.put(prefixKey + entry.getKey(), (String) entry.getValue()); } if (entry.getValue() instanceof Long || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean || entry.getValue() instanceof Float) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(entry.getValue())); } if (entry.getValue() instanceof Double) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(new BigDecimal(String.valueOf(entry.getValue())).longValue())); } if (entry.getValue() instanceof Map) { flatNestedMap.putAll(flatNestedMap(prefixKey + entry.getKey(), (Map<String, Object>) entry.getValue())); } } return flatNestedMap; } }
CommonUtils { @SuppressWarnings("unchecked") public static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap) { Map<String, String> flatNestedMap = new HashMap<String, String>(); String prefixKey = notation != null ? notation + "." : ""; for (Map.Entry<String, Object> entry : nestedMap.entrySet()) { if (entry.getValue() instanceof String) { flatNestedMap.put(prefixKey + entry.getKey(), (String) entry.getValue()); } if (entry.getValue() instanceof Long || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean || entry.getValue() instanceof Float) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(entry.getValue())); } if (entry.getValue() instanceof Double) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(new BigDecimal(String.valueOf(entry.getValue())).longValue())); } if (entry.getValue() instanceof Map) { flatNestedMap.putAll(flatNestedMap(prefixKey + entry.getKey(), (Map<String, Object>) entry.getValue())); } } return flatNestedMap; }  }
CommonUtils { @SuppressWarnings("unchecked") public static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap) { Map<String, String> flatNestedMap = new HashMap<String, String>(); String prefixKey = notation != null ? notation + "." : ""; for (Map.Entry<String, Object> entry : nestedMap.entrySet()) { if (entry.getValue() instanceof String) { flatNestedMap.put(prefixKey + entry.getKey(), (String) entry.getValue()); } if (entry.getValue() instanceof Long || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean || entry.getValue() instanceof Float) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(entry.getValue())); } if (entry.getValue() instanceof Double) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(new BigDecimal(String.valueOf(entry.getValue())).longValue())); } if (entry.getValue() instanceof Map) { flatNestedMap.putAll(flatNestedMap(prefixKey + entry.getKey(), (Map<String, Object>) entry.getValue())); } } return flatNestedMap; }  static Boolean isEnvVariableExists(String envVariableName); static String getEnvVariableValue(String envVariableName); static String doHttpPost(final String url,  String requestBody); static String doHttpPut(final String url, final String requestBody); static boolean isValidResource(String esUrl); static Object buildQueryForExistingIssues(Map<String, Object> filter); static String getIndexNameFromRuleParam(Map<String, String> ruleParam); @SuppressWarnings("unchecked") static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap); static String getUniqueAnnotationId(Annotation annotation); static String getUniqueAnnotationId(String parentId, String ruleId); static String getUniqueIdForString(String inStr); static String hash(String s); static Map<String, String> createParamMap(String ruleParams); static String decrypt(String encryptedText); static Map<String, String> buildMapFromString(String input, String splitOn, String keyValueSeparator); static Long getElapseTimeSince(long startTime); static String getCurrentDateStringWithFormat(String timeZone, String format); static Date getDateFromString(final String dateInString, final String format); static Date dateFormat(final String dateInString, String formatFrom, String formatTo); static int compareDate(final Date firstDate, final Date lastDate); static boolean resourceCreatedBeforeCutoffData(final Date resourceCreationDate); static String doHttpPost(final String url, final String requestBody, final Map<String, String> headers); static String doHttpGet(final String url); static SSLContext createNoSSLContext(); static String getJsonString(final Object annotation); static String encryptB64(String plainText); static String decryptB64(String text); static String encrypt(String plainText, final String key); static String decrypt(String encryptedText, final String key); static String getPropValue(final String keyname); static String getTemplateContent(final String templateName); static String serializeToString(Object object); static Object deSerializeToObject(String jsonString); static Boolean doesAllHaveValue(String...strings ); }
CommonUtils { @SuppressWarnings("unchecked") public static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap) { Map<String, String> flatNestedMap = new HashMap<String, String>(); String prefixKey = notation != null ? notation + "." : ""; for (Map.Entry<String, Object> entry : nestedMap.entrySet()) { if (entry.getValue() instanceof String) { flatNestedMap.put(prefixKey + entry.getKey(), (String) entry.getValue()); } if (entry.getValue() instanceof Long || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean || entry.getValue() instanceof Float) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(entry.getValue())); } if (entry.getValue() instanceof Double) { flatNestedMap.put(prefixKey + entry.getKey(), String.valueOf(new BigDecimal(String.valueOf(entry.getValue())).longValue())); } if (entry.getValue() instanceof Map) { flatNestedMap.putAll(flatNestedMap(prefixKey + entry.getKey(), (Map<String, Object>) entry.getValue())); } } return flatNestedMap; }  static Boolean isEnvVariableExists(String envVariableName); static String getEnvVariableValue(String envVariableName); static String doHttpPost(final String url,  String requestBody); static String doHttpPut(final String url, final String requestBody); static boolean isValidResource(String esUrl); static Object buildQueryForExistingIssues(Map<String, Object> filter); static String getIndexNameFromRuleParam(Map<String, String> ruleParam); @SuppressWarnings("unchecked") static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap); static String getUniqueAnnotationId(Annotation annotation); static String getUniqueAnnotationId(String parentId, String ruleId); static String getUniqueIdForString(String inStr); static String hash(String s); static Map<String, String> createParamMap(String ruleParams); static String decrypt(String encryptedText); static Map<String, String> buildMapFromString(String input, String splitOn, String keyValueSeparator); static Long getElapseTimeSince(long startTime); static String getCurrentDateStringWithFormat(String timeZone, String format); static Date getDateFromString(final String dateInString, final String format); static Date dateFormat(final String dateInString, String formatFrom, String formatTo); static int compareDate(final Date firstDate, final Date lastDate); static boolean resourceCreatedBeforeCutoffData(final Date resourceCreationDate); static String doHttpPost(final String url, final String requestBody, final Map<String, String> headers); static String doHttpGet(final String url); static SSLContext createNoSSLContext(); static String getJsonString(final Object annotation); static String encryptB64(String plainText); static String decryptB64(String text); static String encrypt(String plainText, final String key); static String decrypt(String encryptedText, final String key); static String getPropValue(final String keyname); static String getTemplateContent(final String templateName); static String serializeToString(Object object); static Object deSerializeToObject(String jsonString); static Boolean doesAllHaveValue(String...strings );  }