Skip to content
Advertisement

How can i extract data from json array to use in subsequent rest assure api request

I am using restassured with Java to automate APIs and would like to extract id, recommendId and productId from a GET response to pass into another post requests and would also like to know how to go about extract data from the second object in the array.

I have been struggling to achieve this with the searches I have done so far and appreciate you help on how to go about handling this.

{
   "result":"OK",
   "reason":null,
   "content":[
      {
         "id":"00AX",
         "publishingDate":"2018-05-29",
         "expiryDate":"2018-11-18",
         "title":"Terry William",
         "agentName":"Faith Davis",
         "status":"Viewed",
         "products":[
            {
               "recommendId":"003",
               "code":"9765455",
               "brand":"Gucci",
               "category":"COATS",
               "description":"Trouser",
               "itemStatus":"In Stock",
               "siteStatus":"Approved",
               "reservedStatus":"Recommended",
               "reservedSize":"10 UK",
               "productIDs":[
                  {
                     "productId":"23",
                     "size":"4 UK"
                  },
                  {
                     "productId":"32",
                     "size":"6 UK"
                  },
                  {
                     "productId":"12",
                     "size":"8 UK"
                  },
                  {
                     "productId":"25",
                     "size":"10 UK"
                  },
                  {
                     "productId":"39",
                     "size":"12 UK"
                  },
                  {
                     "productId":"76",
                     "size":"14 UK"
                  }
               ],
               "url":"https://image.jpg"
            }
         ]
      },
      {
         "id":"00AP",
         "publishingDate":"2018-05-29",
         "expiryDate":"2018-11-18",
         "title":"Jones Bob",
         "agentName":"Mary Jones",
         "status":"Viewed",
         "products":[
            {
               "recommendId":"002",
               "code":"6767464",
               "brand":"LV",
               "category":"BAGS",
               "description":"Loewe Bags",
               "itemStatus":"In Stock",
               "siteStatus":"Approved",
               "reservedStatus":"Recommended",
               "reservedSize":"10 UK",
               "productIDs":[
                  {
                     "productId":"23",
                     "size":"4 UK"
                  },
                  {
                     "productId":"32",
                     "size":"6 UK"
                  },
                  {
                     "productId":"12",
                     "size":"8 UK"
                  },
                  {
                     "productId":"25",
                     "size":"10 UK"
                  },
                  {
                     "productId":"39",
                     "size":"12 UK"
                  },
                  {
                     "productId":"76",
                     "size":"14 UK"
                  }
               ],
               "url":"https://image.jpg"
            }
         ]
      }
   ]
}

Advertisement

Answer

Try this,

// Base Test [BaseTest.java]
public class BaseTest {

    protected RequestSpecification requestSpecificationToMerge = new RequestSpecBuilder()
            .setBaseUri("Your Base Url")
            .setContentType(ContentType.JSON)
            .build();

    @BeforeMethod
    public void setFilter() {
        RestAssured.filters(new AllureRestAssured());
    }

}

// Return Id,recommendId and productId [Function.java]
public class ListCompetition extends BaseTest {

public String returnId() {
        return given()
                .spec(requestSpecificationToMerge)
                .basePath("/your endpoint")
                .when()
                .get()
                .getBody()
                .path("content[0].id").toString();
    }

public String recommentId() {
        return given()
                .spec(requestSpecificationToMerge)
                .basePath("/your endpoint")
                .when()
                .get()
                .getBody()
                .path("content[0].products[0].recommendId").toString();
    }

public String productId() {
        return given()
                .spec(requestSpecificationToMerge)
                .basePath("/your endpoint")
                .when()
                .get()
                .getBody()
               .path("content[0].products[0].productIDs[0].productId").toString();
    }
}

You can call this function for your test needed ID, RECOMMENTID AND PRODUCTID

Advertisement