JayWay - Reading the Response -Json Array Response- JsonPath query
JayWay Repsonse:
JsonPath quest
1-
//single attribute : List<?>
List<Object> lst = dc.read("$[?(@.price>54)].price");
//mutlple attribute : List<Map<String,Object>>
List<Map<String, Object>> ListHashThree = dc.read("$[?(@.category=='jewelery')].['title','price','id']");
package
JaywayJsonPath;
import static
io.restassured.RestAssured.given;
import
java.util.List;
import
org.testng.annotations.Test;
import
com.jayway.jsonpath.JsonPath;
import
com.jayway.jsonpath.ReadContext;
import
io.restassured.RestAssured;
import
io.restassured.response.Response;
public class
JayWayJsonResponse {
@Test
public void fakeJayWayTest() {
RestAssured.baseURI
= "https://fakestoreapi.com";
Response response
= given().log().all().when().log().all().get("/products");
String jaywayres =
response.asString();
System.out.println(jaywayres);
ReadContext dc =
JsonPath.parse(jaywayres);
List<Object>
lst = dc.read("$[?(@.price>54)].price");
for (Object e :
lst) {
System.out.println(e);
if(e.equals(114))
{
System.out.println("Sudheer
INN ***");
}
}
@Test
public void ayWayconditionsWithThreeAttributes() {
RestAssured.baseURI = "https://fakestoreapi.com";
Response response = given().log().all().when().log().all().get("/products");
String getResString = response.asString();
DocumentContext dc = JsonPath.parse(getResString);
List<Map<String, Object>> ListHashThree = dc.read("$[?(@.category=='jewelery')].['title','price','id']");
for (Map<String, Object> map : ListHashThree) {
System.out.println(map.get("title"));
System.out.println(map.get("price"));
System.out.println(map.get("id"));
}
}
//$[?((@.category=='jewelery') && (@.price<20))].id
// $[?((@.category=='jewelery') && (@.price<20))].['id','title']
// $[?(@.rating.rate=='4.1')].id
// $[?((@.rating.rate=='4.1') && (@.price>20))].title
@Test
public void ayWayconditionsWithFourAttributes() {
RestAssured.baseURI = "https://fakestoreapi.com";
Response response = given().log().all().when().log().all().get("/products");
String getResString = response.asString();
DocumentContext dc = JsonPath.parse(getResString);
List<Map<String, Object>> ListHashThree = dc
.read("$[?(@.category=='jewelery')].['title','price','id','description']");
for (Map<String, Object> map : ListHashThree) {
System.out.println(map.get("title"));
System.out.println(map.get("price"));
System.out.println(map.get("id"));
System.out.println("description==" + map.get("description"));
}
}
}
}
Comments
Post a Comment