Removing prefix c: from json

This commit is contained in:
Spyros Zoupanos 2020-09-15 21:41:28 +03:00
parent 8bb00add0d
commit 015f6e88df
1 changed files with 38 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
@ -187,15 +188,9 @@ public class SarcStats {
for (Object aJsonArray : jsonArray) {
JSONObject jsonObjectRow = (JSONObject) aJsonArray;
Set<String> jkeys = jsonObjectRow.keySet();
for (String jkey : jkeys) {
System.out.println("++++> " + jkey);
String[] splitArray = jkey.split("c:");
jkey = splitArray[splitArray.length - 1];
System.out.println("New jkey: " + jkey);
}
renameKeysRecursively("c:", jsonObjectRow);
System.out.println("oooo====> " + jsonObjectRow.toJSONString());
fin.write(jsonObjectRow.toJSONString().getBytes());
fin.writeChar('\n');
}
@ -259,6 +254,40 @@ public class SarcStats {
ConnectDB.getConnection().close();
}
private void renameKeysRecursively(String prefixToRemove, JSONArray givenJsonObj) throws Exception {
for(Object jjval : givenJsonObj) {
if(jjval instanceof JSONArray)
renameKeysRecursively(prefixToRemove, (JSONArray)jjval);
else if(jjval instanceof JSONObject)
renameKeysRecursively(prefixToRemove, (JSONObject)jjval);
// All other types of vals
else;
}
}
private void renameKeysRecursively(String prefixToRemove, JSONObject givenJsonObj) throws Exception {
Set<String> jkeys = new HashSet<String>(givenJsonObj.keySet());
for (String jkey : jkeys) {
System.out.println("++++> " + jkey);
String[] splitArray = jkey.split(prefixToRemove);
String newJkey = splitArray[splitArray.length - 1];
System.out.println("New jkey: " + jkey);
Object jval = givenJsonObj.get(jkey);
System.out.println("jval ===> " + jval.getClass().getName());
givenJsonObj.remove(jkey);
givenJsonObj.put(newJkey, jval);
if(jval instanceof JSONObject)
renameKeysRecursively(prefixToRemove, (JSONObject)jval);
if(jval instanceof JSONArray) {
renameKeysRecursively(prefixToRemove, (JSONArray)jval);
}
}
}
private String getJson(String url) throws Exception {
// String cred=username+":"+password;