Sunday, 25 April 2021

How to read String, List and Map in JSON response from API ?

 Check the below link for question and answers with code:

 


@Test(description="Verify status code for GET method-users/2 as 200",groups="SmokeSuite")

public static void verifyResponsejson() throws IOException, org.json.simple.parser.ParseException {

ExtentReport.extentlog = ExtentReport.extentreport.startTest("Execute post method",

" validate post method");

Response resp=given().when().get("https://reqres.in/api/users?page=2");

System.out.println(resp.path("total").toString());

assertEquals(resp.getStatusCode(),200);

 

assertEquals(resp.path("total").toString(),"12");

 

List<String> expected = new ArrayList<String>();

 

List<String> jsonResponse = resp.jsonPath().getList("data");

     System.out.println(jsonResponse.size());

     System.out.println("The number of data in the list is : " + jsonResponse.size());

     assertEquals(jsonResponse.size(),6);

     String usernames = resp.jsonPath().getString("data[1].email");

     System.out.println("Email is : " + usernames);

//   //  List<String> jsonResponses = resp.jsonPath().getList("data");

//   //  System.out.println(jsonResponses.get(0));

     Map<String, String> company = resp.jsonPath().getMap("data[2]");

     System.out.println(company);

     List<String> tset = new ArrayList<>(company.keySet());

     for (int j=0;j<tset.size();j++) {

    System.out.println("The keys for map in data is : "+ tset.get(j));

     }

     System.out.println("Fetch firtsname using map and get: " + company.get("first_name"));

   //  List<Map<String, String>> companies = resp.jsonPath().getList("data");

   //  System.out.println(companies.get(0).get("first_name"));

    List<String> ids = resp.jsonPath().getList("data.email");

//     Collections.sort(jsonResponse);

//     Collections.sort(expected);

//     assertEquals(jsonResponse,expected);

     for(String i:ids)

     {

    System.out.println(i);

        if (i.contentEquals("lindsay.ferguson@reqres.in")) {

        System.out.println("lindsay.ferguson is present in the response body");

        }

     }

}


*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions



No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...