[英]Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured
I am currently calling an API using Rest-Assured, whose response body is posted below.我目前正在使用 Rest-Assured 拨打 API,其响应正文发布在下方。 i need to pick a portion of the response, ie 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.
我需要选择响应的一部分,即整个 WorkItems 标签(第 3 行),并更新字段“CorrelationUId”和“Value”(WorkItemAttributes 标签内的节点),其值需要设置为唯一值。
The updated JSON will serve as a body for another API. How can i achieve this using Rest-Assured and java?更新后的 JSON 将作为另一个 API 的主体。我如何使用 Rest-Assured 和 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.添加了以下依赖项,保证 Rest。 however, i am getting import conflict with jayway dependency "The import io.restassured.path.json.JsonPath collides with another import statement"
但是,我遇到了与 jayway 依赖项的导入冲突“导入 io.restassured.path.json.JsonPath 与另一个导入语句冲突”
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
below snippet helped me out updating the node values and selecting a part of the entire json using Jayway下面的代码片段帮助我更新了节点值并使用 Jayway 选择了整个 json 的一部分
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]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.