'Updating multiple values of different keys in map using java

I have converted a JSON file to a map and I need to modify multiple values of different keys at once using JAVA.

Following is the map converted JSON file:

{
    "name": "",
    "numberID": null,
    "StartDate": "",
    "EndDate": "",
    "count": null,
    "level": null,
    "discipline": null,
    "paymentModel": null,
    "ownerName" : null,
    "coursepackType": null,
    "note": null
}   

I need to update multiple values at once from above JSON, I want to add value to following keys:

  • name
  • StartDate
  • EndDate
  • count
  • ownerName

All I have write now is way to update just single value, which is already pretty linear: Using following series of functions:

String body="";
File testDataJsonfile = new File("path/xxx.json");
        JsonNode testJSONNodes = getJsonNodes(testDataJsonfile);
        Map<String, Object> mapWithJSONNodes = convertJSONTOMAP(testJSONNodes);
        body = updateValue(mapWithJSONNodes, "name", "abc")

public String updateValue(Map<String,Object> map ,String key,String value) throws JsonProcessingException{
            map.put(key, value);
            String convertedJSONFile = new ObjectMapper().writeValueAsString(map);
            return convertedJSONFile;
        }

Any way to to achieve what I want dynamically, probably passing an array list or so? Please let me know if any other specification is required.



Solution 1:[1]

You can modify the method to accept another Map rather than key value pairs and update the existing map with new values present in another map, e.g.

public String updateValue(Map<String,Object> map, Map<String, String> updatedValues) throws JsonProcessingException{
    Map<String, Object> resultMap = new HashMap<>(map);
    resultMap.putAll(updatedValues);
    String convertedJSONFile = new ObjectMapper().writeValueAsString(resultMap);
    return convertedJSONFile;
}

You can call this method with Map, e.g.:

Map<String, String> values = new HashMap<>();
values.put("name", "abc");
values.put("foo", "bar");
body = updateValue(mapWithJSONNodes, "name", "abc")

Also, rather than modifying the map passed in the request you should create a new map and return it (to maintain the state of existing map).

Solution 2:[2]

Most of this code should only be executed once - the parsing of the json file into a Map and the serialization of the updated Map into a JSON String.

Only the put statements should be repeated for each key/value pair you wish to add or modify:

File testDataJsonfile = new File("path/xxx.json");
JsonNode testJSONNodes = getJsonNodes(testDataJsonfile);
Map<String, Object> mapWithJSONNodes = convertJSONTOMAP(testJSONNodes);
mapWithJSONNodes.put ("name", "abc");
mapWithJSONNodes.put (...);
...
String body = new ObjectMapper().writeValueAsString(mapWithJSONNodes);

If you have some array or List containing the keys and values you wish to add, you can replace the put statements with a loop, to reduce the code a bit further.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darshan Mehta
Solution 2