0

Apologies if this is a duplicate post. I am trying to find a string in the following array response basing on conditions specified.

{
    "MRData": {
        "xmlns": "http://ergast.com/mrd/1.4",
        "series": "f1",
        "url": "http://ergast.com/api/f1/2016/drivers.json",
        "limit": "30",
        "offset": "0",
        "total": "24",
        "DriverTable": {
            "season": "2016",
            "Drivers": [
                {
                    "driverId": "alonso",
                    "permanentNumber": "14",
                    "code": "ALO",
                    "url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
                    "givenName": "Fernando",
                    "familyName": "Alonso",
                    "dateOfBirth": "1981-07-29",
                    "nationality": "Spanish"
                },
                {
                    "driverId": "bottas",
                    "permanentNumber": "77",
                    "code": "BOT",
                    "url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
                    "givenName": "Valtteri",
                    "familyName": "Bottas",
                    "dateOfBirth": "1989-08-28",
                    "nationality": "Finnish"
                },
                {
                    "driverId": "button",
                    "permanentNumber": "22",
                    "code": "BUT",
                    "url": "http://en.wikipedia.org/wiki/Jenson_Button",
                    "givenName": "Jenson",
                    "familyName": "Button",
                    "dateOfBirth": "1980-01-19",
                    "nationality": "British"
                  }
            ]
        }
    }
}

1) I would like to find the permanent number of driverId "alonso" assuming that it doesn't come first always in each request. i.e each time the request is made the arrays reshuffle. the logic here would be to get the array count of the driverId alonso and insert that into the query below

"MRData.DriverTable.Drivers[insert the array count of alonso here].permanentNumber"

2) I would like to get the permanent numbers that are less than 20. I would also like to get the driverIds of the drivers whose permanent numbers are less than 20.

thanks a lot for viewing!

Raj
  • 95
  • 1
  • 3
  • 13
  • How this question related to selenium? – Prophet Apr 01 '18 at 10:10
  • 2
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Lars Apr 01 '18 at 10:15
  • Thanks guys! Will have a look. Apologies of the selenium tag, it's removed now. – Raj Apr 01 '18 at 10:23
  • just wondering if there a way to write a path, some thing similar to GPath in groovy to get what i wanted, with out looping around array objects. – Raj Apr 01 '18 at 15:41

3 Answers3

0

Try to build the Classes "MRData" and "Driver" with all necessary parameters.

and let org.json or GSON do the magic. You should really look at How to parse JSON in Java as Lars mentioned.

0
got that sorted!
answer to my first question-

    public void extraResponseWithInRange(String url) {
            Response response = given().when().get(url);
            List<Map<String, String>> responseFromArray = JsonPath.parse(response.asString()).read("$.MRData.DriverTable.Drivers[?(@.driverId== 'alonso')]");
            for (Map<String, String> rfa : responseFromArray) {
                assertThat(rfa.get("permanentNumber"), equalToIgnoringCase("14"));

answer to my second question-

 List<Map<String,String>> driversBetween=JsonPath.parse(response.asString()).read("$.MRData.DriverTable.Drivers[?(@.permanentNumber > '0' && @.permanentNumber <'20')]");
            for(Map<String,String> dbsmall: driversBetween){
                System.out.println(dbsmall.get("permanentNumber"));

            }

please let me know if i could write this in a better way. thanks a lot!

Raj
  • 95
  • 1
  • 3
  • 13
0

Either marshall the data into a POJO, and check the values of the fields there, or use something like [JSONPath][1].

int permanentNumber = JSONPath.read(json, "$..Drivers[?(@.driverId == 'alonso')].permanentNumber");

Disclaimer, I don't have an environment currently to run this, but their docs are pretty good.

Xetius
  • 44,755
  • 24
  • 88
  • 123