1

I have below json and need to update elements, below code works for elements in top level, How can I extend this to work it inside another inner level (object).

Json:

{
  "name": George,
  "version": "2.0",
  "reqParams": {
    "headerId": "this needs to be updated",
    "queue": "draft",
  }
}

In below code I am passing below

eg. keyPath = "headerId" updateText = "123456" jsonText = above json

Code :

public String updateValue(String keyPath, String updateText, String jsonText) {
    String[] keys = keyPath.split("/");
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
    JsonObject returnVal = jsonObject; // This holds the ref to target json object
    JsonPrimitive jp = new JsonPrimitive(updateText);
    String finalKey = keys[keys.length - 1];
    for(String key : keys)
    {
        if (jsonObject.get(key) != null && jsonObject.get(key).isJsonObject())
        {
            jsonObject = (JsonObject)jsonObject.get(key);
        }
    }
    jsonObject.remove(finalKey);
    jsonObject.add(finalKey, jp);
    return returnVal.toString();
}

Code

Expected out put json:

{
  "name": George,
  "version": "2.0",
  "reqParams": {
    "headerId": "123456",
    "queue": "draft",
  }
}

Actual reult:

{
  "name": George,
  "version": "2.0",
  "reqParams": {
    "headerId": "this needs to be updated",
    "queue": "draft",
  },
  "headerId": "123456",
}
Shabar
  • 2,617
  • 11
  • 57
  • 98

2 Answers2

1

Pass keyPath as "reqParams/headerId" because headerId is inside reqParams and not at root level of JSON.

Smile
  • 3,832
  • 3
  • 25
  • 39
  • Is there a way to traverse through all child objects and update. Because some payloads it is in root level. Otherwise need to hardcore the keypath based on each payload. Sometimes "headerId" can be in both root level and child object level – Shabar Feb 17 '20 at 08:57
  • 1
    Check this answer https://stackoverflow.com/a/10593838/1776132 to deep traverse json and tweak the code to update the value depending on the key. – Smile Feb 17 '20 at 09:09
0

Updated code slightly and pass parameters as suggested by @Smile answer

keyPath : reqParams/headerId
          someId (if exist in root level)

Code :

public String updateValue(String keyPath, String updateText, String jsonText) {
    String[] keys = keyPath.split("/");
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
    JsonObject returnVal = jsonObject; // This holds the ref to target json object
    JsonPrimitive jp = new JsonPrimitive(updateText);
    String finalKey = keys[keys.length - 1];
    for (String key : keys) {

    if (jsonObject.get(key) != null && jsonObject.get(key).isJsonObject()) {
        jsonObject = (JsonObject) jsonObject.get(key);
        jsonObject.remove(finalKey);
        jsonObject.add(finalKey, jp);
        return returnVal.toString();
    } else if (jsonObject.get(finalKey) == null) {
        return returnVal.toString();
    }
}

jsonObject.remove(finalKey);
jsonObject.add(finalKey, jp);
return returnVal.toString();

}

Shabar
  • 2,617
  • 11
  • 57
  • 98