I know a similar question has been posted, however, the answer didn't work for me.
I am using RestAssured. How do I convert my Response
into a JSON object and then extract a field that is within the JSON array (in my case, inside the errors
array)?
Response response =
given().
contentType(ContentType.JSON).
body(jsonAsMap).
when().
post("/customers").
then().
statusCode(400).extract().response();
//get "code" field inside "errors".
When testing the request in Postman, I get this result:
{
"uri": "/customers/",
"query": null,
"method": "POST",
"contentType": "application/json",
"status": 400,
"statusMessage": "Bad Request",
"errors": [
{
"field": "firstName",
"code": "firstName.required", //need to extract this field
"message": "firstName is required"
}
]
}
What is the easiest way to achieve this?
You can use JsonPath to access the value and convert this into a Java type.
Something like this:
String errorCode = ...then().extract().jsonPath().getString( "errors[0].code" )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.