6

The function contains the API in which I can get the value in JSON format. But I want to get the exact ID=2 which is present in JSON How can I get that data using the Java JSON path. I have to use the maven dependency. The function contains the API in which I can get the value in JSON format. But I want to get the exact ID=2 which is present in JSON How can I get that data using the Java JSON path. I have to use the maven dependency.


    <dependency>
       <groupId>com.jayway.jsonpath</groupId>
       <artifactId>json-path</artifactId>
       <version>2.4.0</version>
   </dependency>

 

     void get_extract_response_body() {
          //Specify Base URL
          
          RestAssured.baseURI="https://reqres.in/api";
          
          //Request object
          RequestSpecification httpRequest=RestAssured.given(); 
          //Response Object
          Response response=httpRequest.request(Method.GET,"/unknown");
          
          String responseBody= response.getBody().asString();
          System.out.println(responseBody);
          
          //Print Response in console window
          JsonPath jsonpath= response.jsonPath(); 
          
      System.out.println(jsonpath.get("data[0]."));
      
      
          /*
           * String id=jsonpath.get("$.data[1].id"); System.out.println(id);
           */
          
          
          
      }


  

   {
       "page": 1,
       "per_page": 6,
       "total": 12,
       "total_pages": 2,
       "data": [
           {
               "id": 1,
               "name": "cerulean",
               "year": 2000,
               "color": "#98B2D1",
               "pantone_value": "15-4020"
           },
           {
               "id": 2,
               "name": "fuchsia rose",
               "year": 2001,
               "color": "#C74375",
               "pantone_value": "17-2031"
           },
           {
               "id": 3,
               "name": "true red",
               "year": 2002,
               "color": "#BF1932",
               "pantone_value": "19-1664"
           },
           {
               "id": 4,
               "name": "aqua sky",
               "year": 2003,
               "color": "#7BC4C4",
               "pantone_value": "14-4811"
           },
           {
               "id": 5,
               "name": "tigerlily",
               "year": 2004,
               "color": "#E2583E",
               "pantone_value": "17-1456"
           },
           {
               "id": 6,
               "name": "blue turquoise",
               "year": 2005,
               "color": "#53B0AE",
               "pantone_value": "15-5217"
           }
       ],
       "support": {
           "url": "https://reqres.in/#support-heading",
           "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
       }
   }


This Response helped me to find the key-value from each data.

1 Answer 1

7

There are 2 JsonPath:

  • JsonPath Rest-Assured:
JsonPath jsonpath= response.jsonPath();
int id = jsonPath.getInt("data[1].id");
System.out.println(id); //2
  • JsonPath Jayway:
int id = com.jayway.jsonpath.JsonPath.read(response.asString(), "$.data[1].id");
System.out.println(id); //2
Sign up to request clarification or add additional context in comments.

2 Comments

instead of read how can I put ?
@Alok don't know what you mean, pls submit a question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.