I need to convert a AWS DYNAMODB JSON to a standard JSON object. so I can remove the data type from the DynamoDB JSON Something more like:
in DYNAMODB JSON:
Convert your JSON string to a Map first using
JSONObject jsonObj = new JSONObject(logString);
HashMap myMap = new Gson().fromJson(jsonObj.toString(), HashMap.class);
Function for converting DynamoDB JSON to Standard JSON
public static Map mapToJson(Map map){
Map finalKeyValueMap = new HashMap();
Iterator it = map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Map obj1 = (Map) pair.getValue();
if(obj1.get("m") == null) {
if(obj1.get("n") != null)
finalKeyValueMap.put(pair.getKey().toString(),obj1.get("n"));
else if(obj1.get("s") != null)
finalKeyValueMap.put(pair.getKey().toString(),obj1.get("s"));
}
else {
Map obj2 = (Map) pair.getValue();
Map obj3 = (Map) obj2.get("m");
finalKeyValueMap.put(pair.getKey().toString(),mapToJson(obj3));
}
}
System.out.println(finalKeyValueMap.toString());
return finalKeyValueMap;
}
calling mapToJson(myMap); will return a standard Map which you can convert back to JSON