Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove nested key from a json object using java

Tags:

java

json

I have the following json structure

{  
   "MerchHierarchyEBM":{  
      "DataArea":{  
         "Division":{  
            "UpdatedBy":"SN",
            "Group":{  
               "GroupName":"Womens Fashion*",
               "UpdatedBy":"Data Migration",
               "UpdatedOn":"22-NOV-17",
               "GroupID":"200"
            },
            "DivisionName":"Fashion",
            "UpdatedOn":"22-NOV-17",
            "DivisionID":"2000"
         }
      }
   }
}

and i want to remove the "Group" key and value from the json object using java i tried few things but didn't work following is my code .

JSONObject jsonObjIncomingDatanew =new JSONObject(Result);
jsonObjIncomingDatanew.remove("MerchHierarchyEBM.DataArea.Division.Group");
like image 575
smn_onrocks Avatar asked Oct 16 '25 22:10

smn_onrocks


1 Answers

Try this:

JSONObject jsonObject = new JSONObject(Result);
jsonObject
  .getJSONObject("MerchHierarchyEBM")
  .getJSONObject("DataArea")
  .getJSONObject("Division")
  .remove("Group");

Or if getJSONObject() doesn't work, replace it with getAsJsonObject().

like image 117
Omar Einea Avatar answered Oct 18 '25 11:10

Omar Einea