public static org.json.JSONObject cleanJson(org.json.JSONObject jsonObject) throws JSONException {
Iterator <String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
if (jsonObject.has(key)) {
Object val = jsonObject.get(key);
if (val instanceof String) {
if (((String) val).isEmpty()) {
jsonObject.remove(key);
}
} else if (val instanceof org.json.JSONObject) {
JSONObject jObj = cleanJson((JSONObject) val);
jsonObject.put(key, jObj);
} else if (val instanceof org.json.JSONArray) {
JSONArray jsonArray = (JSONArray) val;
if (jsonArray.length() == 0) {
jsonObject.remove(key);
} else {
for(int i = 0; i < jsonArray.length(); i++) {
jsonArray.put(i, cleanJson((JSONObject) jsonArray.get(i)));
}
}
}
}
}
return jsonObject;
}
Last active
July 25, 2020 17:29
-
-
Save thelostone-mc/5deb6f200fb1190818ddbc45934ccb51 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment