Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

What is a jsonPath in the context of REST Assured?

Category: Rest Assured

A JsonPath (io.restassured.path.json.JsonPath) is an easy way to get values from an Object document without resorting to XPath. It conforms to the Groovy GPath syntax when it retrieves an object from a document. Consider it a JSON-specific version of XPath. Here’s an example:

{
   "company":{
      "employee":[
         {
            "id":1,
            "name":"User1",
            "role":"Admin"
         },
         {
            "id":2,
            "name":"User2",
            "role":"User"
         },
         {
            "id":3,
            "name":"User3",
            "role":"User"
         }
      ]
   }
}
Response employeesResponse = RestAssured.given().request(Method.GET, "/all");

JsonPath jsonPathObj = employeesResponse.jsonPath();

//get a list of all employees id:

List employeeIds = jsonPathObj.get("company.employee.id");

//get the first employee name:

String empName = jsonPathObj.get("company.employee[0].name");

Leave a Reply

Your email address will not be published. Required fields are marked *