Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured

1.5k views Asked by At

I am currently calling an API using Rest-Assured, whose response body is posted below. i need to pick a portion of the response, i.e. whole of WorkItems tag(at line 3), and update the fields "CorrelationUId" and "Value"(node within WorkItemAttributes tag) whose value needs to be set to a unique value.

The updated JSON will serve as a body for another API. How can i achieve this using Rest-Assured and java?

   {
    "TotalRecordCount": 1,
    "BatchSize": 500,
    "WorkItems": [{
        "CreatedByApp": "IssueManagement",
        "ItemState": 1,
        "StackRank": 0,
        "CorrelationUId": "05c0df91-cd6f-4f74-8e19-0be556879e59",
        "RowStatus": null,
        "WorkItemDeliveryConstructs": [{
                "CreatedByUser": "Gateway",
                "ItemState": 0
            }

        ],

        "WorkItemLanguages": null,
        "WorkItemProductInstances": [{
            "ModifiedOn": "2020-08-05T05:01:15.335316Z",
            "UserUId": null,
            "ItemState": 0
        }],
        "WorkItemAssociations": null,
        "WorkItemAttachments": null,
        "WorkItemAttributes": [{

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "enter unique data here",
                "ItemState": 0
            },

            {

                "IdValue": "00000000-0000-0000-0000-000000000000",
                "IdExternalValue": "",
                "Value": "",
                "ItemState": 0
            }
        ]


    }],
    "Faults": [],
    "StatusCode": 0,
    "MergeResult": null
}

Below is the code snippet for the above.

 RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json")
 JSONObject requestParams = new JSONObject();
requestParams.put("data", Property.getProperty("data")); 
Response response = request.post(url);
JsonPath js = response.jsonPath();
JSONObject responseObject = new JSONObject(response.jsonPath().getJsonObject("WorkItems"));
 
 
Configuration configuration = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build();
DocumentContext json = JsonPath.using(configuration).parse(jsonString);
String jsonPath = "WorkItems.CorrelationUId";
String newValue = "newCorrelationUId";
System.out.println(json.set(jsonPath, newValue).jsonString()); 

Added the below dependency with Rest assured. however, i am getting import conflict with jayway dependency "The import io.restassured.path.json.JsonPath collides with another import statement"

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>
1

There are 1 answers

1
Sonal On

below snippet helped me out updating the node values and selecting a part of the entire json using Jayway

  DocumentContext json = JsonPath.using(configuration).parse(file);
   String jsonPath  = "WorkItems[0].WorkItemAttributes[0].Value";
String newValue = "new title";
DocumentContext finaljson = json.set(jsonPath, newValue);
 DocumentContext context = JsonPath.parse(finaljson.jsonString());
HashMap<String, Object> requiredpartofJson= context.read("WorkItems[0]");