Through below code able to replace or remove the given item.
public JSONObject updateOrRemoveJsonProperty(Object js1, String keys, Object valueNew, ConfigData.JsonBuildType payloadEnum, String targetKey){
try {
List<String> keyMain = new LinkedList<String>(Arrays.asList(keys.split("\\.")));
for (int i = 0; i < keyMain.size(); i++) {
if(js1 instanceof JSONObject || js1 instanceof JSONArray){
if(keyMain.size() >1 && i+1 < keyMain.size()) {
String tmpKey= "";
String stringArray ="";
try {
tmpKey = keyMain.get(i);
keyMain.remove(i);
stringArray = StringUtils.join(keyMain, ".");
keyMain.clear();
updateOrRemoveJsonProperty(((JSONObject) js1).get(tmpKey), stringArray, valueNew, payloadEnum, targetKey);
}catch (JSONException js){
if(!tmpKey.isEmpty() && tmpKey.matches(KeyMapper.KEYLSTREGX)){
String[] tmp = tmpKey.replaceFirst(KeyMapper.KEYLSTREGX, "$1, $2").split(",");
try {
updateOrRemoveJsonProperty((JSONArray)(((JSONArray)((JSONObject) js1).get(tmp[0])).get(Integer.parseInt(tmp[1].trim()))), stringArray, valueNew, payloadEnum, targetKey);
}catch (ClassCastException ex){
updateOrRemoveJsonProperty((JSONObject)(((JSONArray)((JSONObject) js1).get(tmp[0])).get(Integer.parseInt(tmp[1].trim()))), stringArray, valueNew, payloadEnum, targetKey);
}
}
}
} else {
if((keyMain.get(i)).length() > 2 && keyMain.get(i).matches(KeyMapper.KEYLSTREGX)){
String[] tmp = keyMain.get(i).replaceFirst(KeyMapper.KEYLSTREGX, "$1, $2").split(",");
if(targetKey != "" && tmp[0].trim().equals(targetKey) && !payloadEnum.equals(ConfigData.JsonBuildType.REMOVE)) {
((JSONObject) js1).put(tmp[0], valueNew);
} else if (targetKey != "" && tmp[0].trim().equals(targetKey) && payloadEnum.equals(ConfigData.JsonBuildType.REMOVE)){
((JSONObject) js1).remove(tmp[0]);
}
}
if(targetKey != "" && keyMain.get(i).equals(targetKey) && !payloadEnum.equals(ConfigData.JsonBuildType.REMOVE)) {
((JSONObject) js1).put(keyMain.get(i), valueNew);
} else if (targetKey != "" && keyMain.get(i).equals(targetKey) && payloadEnum.equals(ConfigData.JsonBuildType.REMOVE)){
((JSONObject) js1).remove(keyMain.get(i));
}
}
}
}
}catch (JSONException ex){
}
return (JSONObject) js1;
}