2

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"
    }
}
dkb
  • 4,389
  • 4
  • 36
  • 54
Luis Urea
  • 107
  • 3
  • 12
  • Which JSON parsing library are you using? And which library provides the JsonPath implementation, if they aren't the same? – Aaron Apr 09 '19 at 12:16
  • import io.restassured.path.json.JsonPath; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; – Luis Urea Apr 09 '19 at 12:18

1 Answers1

6

com.jayway.jsonpath.DocumentContext.set() can be used to modify the value of an element in JSON

   /**
     * Set the value a the given path
     *
     * @param path      path to set
     * @param newValue  new value
     * @return a document context
     */
    DocumentContext set(JsonPath path, Object newValue);

Library:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version><!--Version--></version>
</dependency>

Code Snippet:

String json = "{\n" +
                "\t\"contract\": {\n" +
                "\t\t\"marketScope\": \"AT\",\n" +
                "\t\t\"businessPartner\": \"GBM\",\n" +
                "\t\t\"salesChannelInformation\": {\n" +
                "\t\t\t\"salesChannelCode\": \"Integrated\",\n" +
                "\t\t\t\"salesChannel\": \"B-Partner information 1\"\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
        DocumentContext parsedDataContext = jsonParseContext.parse(json);

        parsedDataContext.set("$..contract.salesChannelInformation.salesChannelCode", "Integrated-Test");

        System.out.println(parsedDataContext.jsonString());

Output:

{"contract":{"marketScope":"AT","businessPartner":"GBM","salesChannelInformation":{"salesChannelCode":"Integrated-Test","salesChannel":"B-Partner information 1"}}}
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
  • Careful.the above code will update the regardless of the type of node. i.e. it would update the entire array to one string value if attribute is array type. – iAutomate Jul 09 '22 at 07:35