I have a json file with the following example structure
{
"contract": {
"marketScope": "AT",
"businessPartner": "GBM",
"salesChannelInformation": {
"salesChannelCode": "Integrated",
"salesChannel": "B-Partner information 1"
}
}
Giving a jsonpath, I would like to modify an specific key-value.
for example Change "contract.salesChannelInformation.salesChannelCode" with the value "Integrated-Test"
For the moment I have the following code:
public void setProperty(String fileString,String path, String value) {
if(JsonPath.given(fileString).get(path) == null){
Assert.fail("Path does not exist on json file");
}else {
try {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(fileString);
System.out.println(jsonObject);
String[] tokens = path.split("\\.");
for (String token : tokens) {
System.out.println(token);
// Iterate the JsonObject, reach the key and modify the value
}
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
I expect to modify the json file in this way
{
"contract": {
"marketScope": "AT",
"businessPartner": "GBM",
"salesChannelInformation": {
"salesChannelCode": "Integrated-Test",
"salesChannel": "B-Partner information 1"
}
}