Parse Json Recursively and manipulate the object value

Viewed 50

I've a complex json object which i want to iterate and replace a specific key values. The key value to be replaced will be under rules -> field -> value and inside the value based on the id I've to replace the name. For instance i have a json object { "id" : "123" , "name" : "abc" } i want to replace with { "id" : "123" , "name" : "xyz" }

Input :

{
"condition": {
    "rules": [{
        "field": "stage.id",
        "value": [{
                "condition": [{
                    "rules": [{
                        "field": "stage.id",
                        "value": [{
                            "condition": [{
                                "rules": [{
                                    "field": "stage.id",
                                    "value": [{
                                        "id": "123",
                                        "name": "abc",
                                        "item": {
                                            "id": "456",
                                            "name": "ffgg"
                                        }
                                    }]
                                }]
                            }]
                        }]
                    }]
                }]
            },
            {
                "field": "tasks.stage.id",
                "value": [{
                    "id": "456",
                    "name": "def"
                }]
            },
            {
                "field": "stage.id",
                "value": [{
                    "id": "123",
                    "name": "abc"
                }]
            }
        ]
    }]
}

Expected Output:

{
    "condition": {
        "rules": [{
            "field": "stage.id",
            "value": [{
                    "condition": [{
                        "rules": [{
                            "field": "stage.id",
                            "value": [{
                                "condition": [{
                                    "rules": [{
                                        "field": "stage.id",
                                        "value": [{
                                            "id": "123",
                                            "name": "xyz",
                                            "item": {
                                                "id": "456",
                                                "name": "ffgg"
                                            }
                                        }]
                                    }]
                                }]
                            }]
                        }]
                    }]
                },
                {
                    "field": "tasks.stage.id",
                    "value": [{
                        "id": "456",
                        "name": "def"
                    }]
                },
                {
                    "field": "stage.id",
                    "value": [{
                        "id": "123",
                        "name": "xyz"
                    }]
                }
            ]
        }]
    }

I've iterated to the object based in the instance but replacing the value is not working as expected. I'm not sure what i've missed here. Can someone help me in here.

Code Sample

   try {      
   loopThroughJson(new JSONObject(theInput)));
} catch (JSONException e) {
  e.printStackTrace();
}

public static String loopThroughJson(Object input) throws JSONException {    
if (input instanceof JSONObject) {
  Iterator<?> keys = ((JSONObject) input).keys();
  while (keys.hasNext()) {
    String key = (String) keys.next();
    if (!(((JSONObject) input).get(key) instanceof JSONArray))
      if (((JSONObject) input).get(key) instanceof JSONObject) {
        loopThroughJson(((JSONObject) input).get(key));
      } else
       System.out.println(key + "=" + ((JSONObject) input).get(key));
    else        
      loopThroughJson(new JSONArray(((JSONObject) input).get(key).toString()));        
  }
}

if (input instanceof JSONArray) {
  Gson gson = new Gson();

  for (int i = 0; i < ((JSONArray) input).length(); i++) {
    JSONObject a = ((JSONArray) input).getJSONObject(i);       
    if (a.opt("field") != null && a.opt("field").equals("stage.id")) {
      Map<String, Object> fieldJson = gson.fromJson(a.toString(), Map.class);
      List<Map<String, Object>> valueArray = (List<Map<String, Object>>) fieldJson.get("value");
      for (Map<String, Object> valueObject : valueArray) {
        if (valueObject.get("id").toString().equals("123")) {
          valueObject.put("name", "xyz");
        }
      }
      a = new JSONObject(fieldJson);          
    }       
      loopThroughJson(a);
    }

}    
return input.toString();
0 Answers
Related