0

I want to parse through my JSON response to validate the response i obtained.

 [
    {
        "Emp": "00000",
        "Emp_ID": 901,
        "First_Name": "agar",
        "Last_Name": "vedi",
        "Country": "India",
        "EmpLocation": "Noida"
    },
    {
        "Emp": "001",
        "Emp_ID": 383,
        "First_Name": "Manoj",
        "Last_Name": "jee"
        "Country": "India",
        "EmpLocation": "Noida"
    }
]

Now , I am using Rest-assured java API for this , I went through the tutorial on toolsQA and their they are using

Response response = httpRequest.request(Method.GET, "/Hyderabad");

For the Json:-

{
    “City”: “Hyderabad”,
    “Temperature”: “31.49 Degree celsius”,
    “Humidity”: “62 Percent”,
    “Weather Description”: “scattered clouds”,
    “Wind Speed”: “3.6 Km per hour”,
    “Wind Direction degree”: “270 Degree”
   }

Now this response is a single JSON object. But mine is nested in a JSON Array .

How do I parse through such nested Json objects and arrays ? as Json response can come in all combinations of Array and Objects .

Is their a method in rest-assured which provides value corresponding to the key? example: "key":"value" I go to the key and obtain the value through that method?

Thanks.

aditya rawat
  • 129
  • 2
  • 18

1 Answers1

1

If you have mapped the JSON to PoJo, you just need to change the request to:

City[] cities = given()
                    .when()
                    .get("/Hyderabad")
                    .then()
                    .response()
                    .getBody()
                    .as(City[].class);

This is how RestAssured can deserialize an array of objects. You need to map it to Array of Objects.

Kristijan Rusu
  • 567
  • 4
  • 14