private static void removeNullFields(Object object) throws JSONException {
  if (object instanceof JSONArray) {
    JSONArray array=(JSONArray)object;
    for (int i=0; i < array.length(); ++i)     removeNullFields(array.get(i));
  }
 else   if (object instanceof JSONObject) {
    JSONObject json=(JSONObject)object;
    JSONArray names=json.names();
    if (names == null)     return;
    for (int i=0; i < names.length(); ++i) {
      String key=names.getString(i);
      if (json.isNull(key)) {
        json.remove(key);
      }
 else {
        removeNullFields(json.get(key));
      }
    }
  }
}

private static void removeNullFields ( Object object ) throws JSONException { if ( object instanceof JSONArray ) { JSONArray array = ( JSONArray ) object ; for ( int i = 0 ; i < array . length ( ) ; ++ i ) removeNullFields ( array . get ( i ) ) ; } else if ( object instanceof JSONObject ) { JSONObject json = ( JSONObject ) object ; JSONArray names = json . names ( ) ; if ( names == null ) return ; for ( int i = 0 ; i < names . length ( ) ; ++ i ) { String key = names . getString ( i ) ; if ( json . isNull ( key ) ) { json . remove ( key ) ; } else { removeNullFields ( json . get ( key ) ) ; } } } }
remove ' null ' fields from a jsonobject . this method calls itself recursively until all the fields have been looked at . todo ( mlamouri ) : move to some util class ?
