I have a property file, where the user can define full field names, something like:
league.firstname
league.lastName
Now in my code, I have an incoming JSON, and I want to first retrieve the existing value and then edit the values of fields mentioned in the property file. In this case, retrieve and edit values of league.firstName and league.lastName fields. JSON looks something like:
{
"_internal": {
"pubDateTime": "2017-12-10 11:42:23.504",
"xslt": "xsl/league/roster/marty_active_players.xsl",
"eventName": "league_roster"
},
"league": {
{
"firstName": "Alex",
"lastName": "Abrines",
"personId": "203518",
"teamId": "1610612760"
}
}
}
Now I know I can do something like:
// retrieve values
String fName = jsonObj.getJSONObject("league").getString("firstName");
String lName = jsonObj.getJSONObject("league").getString("lastName");
// put new values
jsonObj.getJSONObject("league").put("firstName", "newVal1");
jsonObj.getJSONObject("league").put("lastName", "newVal2");
But how do I achieve this dynamically. For example, property file is like:
parentObj.child1.child2.child3
and now I want to edit this 4 level deep JSON, so want to do something like:
// how to do the following
String oldVal = jsonObj.getString("parentObj.child1.child2.child3");
jsonObj.put("parentObj.child1.child2.child3", "newVal");
Note:
- the level of json depth can be 1 < length < 100.
- user can put any relevant value in property file. If the key do not exist in JSON, need to catch exception.
- incoming json format is NOT fixed.