Jim
Jim

Reputation: 4415

How can I structurally modify a JsonObject and replace some of its values?

I have a json in a JsonObject such as:

"customer": {    
     "full_name": "John John",  
      "personal": {  
        "is_active": "1",    
        "identifier_info": {  
            "hobbies": [  
                "skiing",
                "traveling"
             ],  
             "user_id": "1234",  
             "office_id": "2345"
         }  
     }  
} 

Is there a way to modify the JsonObject so as to remove the hobbies completely from the identifier_info and leave the rest untouched and then add the contents of hobbies the same way as the others? i.e.

"customer": {    
     "full_name": "John John",  
      "personal": {  
        "is_active": "1",    
        "identifier_info": {  
            "skiing":"expert",
            "traveling":"rarely",
             "user_id": "1234",  
             "office_id": "2345"                                                                                  
         }
     }
}  

Upvotes: 2

Views: 3739

Answers (2)

Sree
Sree

Reputation: 1758

Find the full implementation for removing the JSON Array "hobbies" and to insert them in the parent JSON Object directly.

  public class ProcessJSONString {

  String data ="{\"customer\": {    \n" +
        "     \"full_name\": \"John John\",  \n" +
        "      \"personal\": {  \n" +
        "        \"is_active\": \"1\",    \n" +
        "        \"identifier_info\": {  \n" +
        "            \"hobbies\": [  \n" +
        "                \"skiing\",\n" +
        "                \"traveling\"\n" +
        "             ],  \n" +
        "             \"user_id\": \"1234\",  \n" +
        "             \"office_id\": \"2345\"\n" +
        "         }  \n" +
        "     }  \n" +
        "}} ";

public void processData() {
    try {
        JSONObject result = new JSONObject(data);
        JSONObject customer = result.getJSONObject("customer");
        JSONObject personal = customer.getJSONObject("personal");
        JSONObject identifierInfo = 
        personal.getJSONObject("identifier_info");
        JSONArray hobbies = identifierInfo.getJSONArray("hobbies");

        identifierInfo.remove("hobbies");

         //Under the assumption the tags will be added by the user, the code has been inserted.
        identifierInfo.put("skiing","expert");
        identifierInfo.put("traveling","rarely");



    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    ProcessJSONString instance = new ProcessJSONString();
    instance.processData();
}
}

Upvotes: 3

nosyjoe
nosyjoe

Reputation: 561

That should be no problem, using JsonObject's remove() method. It returns the removed JsonElement. Assuming the original json is a JsonObject called customer:

JsonObject identifierInfo = customer.getAsJsonObject("personal").getAsJsonObject("identifier_info");
JsonArray hobbies = (JsonArray) identifierInfo.remove("hobbies");

after that you can just add the hobbies to the identifierInfo and get the desired result:

for (JsonElement aHobby : hobbies) {
    identifierInfo.addProperty(aHobby.getAsString(), "expert");
}

don't forget to add null checks as needed.

Upvotes: 2

Related Questions