Posted in Automation Testing

JSONPath and XML Path using Rest assured

While working with API test automation we use tools like rest assured with Java.
We have many ways to parse json and extract values but rest assured has in-built methods which supports json value extraction from Response object.

Example using jsonPath() :
To extract the email from below response in rest assured:

{
    "id": "89094da8-4bdf-42f5-a767-5b347070342e",
    "email": "user_any01@yopmail.com",
    "firstName": "AdminUser",
    "lastName": "UMS",
    "country": "IN",
}
String email = given()
                .when()
                .get("/user")
                .then()
                .getBody().jsonPath().get("email");

Example using xmlPath() :
To extract the email from below response in rest assured:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<User>
    <country>IN</country>
    <email>user_any01@yopmail.com</email>
    <firstName>AdminUser</firstName>
    <id>89094da8-4bdf-42f5-a767-5b347070342e</id>
</User>
String email = given()
                .when()
                .get("/user")
                .then()
                .getBody().xmlPath().get("User.email");

Leave a comment