Search code examples
rest-assuredrest-assured-jsonpath

How to get the value from a List using Rest Assured?


I am trying to grab the first value of courseNumber for studenId=123 using Rest Assured.

When I using

.body("students.courseList.courseNumber[0]",equalTo(1000000000))

I am getting:

Expected: <1000000000>
Actual: [1000000000, 1000000001, 1000000002, 1000000003, ...........]

There are more than 10 courseList for studentId 123. Also is it possible to use regex to grab a particular element from JSON Response or how do I get the path of an element when I have a few thousand lines of JSON Response.

Sample Response -

        {
            "students": [
                 {
                    "studentId": "ABC",
                    "studentName": "Abcd Abcd",
                    "courseDescription": "AAJSHKJASSJAK LASNLKASA KJk;;K K;;KK;K;KL;K;",
                    "creditRemaining": 100,
                    "classStartDate": "20191220"
                },
                {
                    "studentId": "123",
                    "studentName": "DEFG, VBNH",
                    "courseDescription": "AAJSHKJASSJAK LASNLKASA KJk;;K K;;KK;K;KL;K;",
                    "classSchedule": 2,
                    "classStartDate": "20191220",
                    "slotsRemaining": 10,
                    "courseList": [
                        {
                            "courseNumber": 1000000000,
                            "courseName": "Chemistry",
                            "courseInstructor": "HGJ IOUIOU"
                            "courseCity": "New York",
                            "courseLevel": 100,
                            "description": "GJKJLKJLafgdhgf ljkllklk klyiyy mnbbnkljlkj 
       yttiuyuyuyoyo 
                         jhlkjkljkl"},
                    {
                        "courseNumber": 1000000001,
                        "courseName": "History",
                        "courseInstructor": "HGJ IOUIOU"
                        "courseCity": "New York",
                        "courseLevel": 100,
                        "description": "GJKJLKJLafgdhgf ljkllklk klyiyy mnbbnkljlkj yttiuyuyuyoyo 
                         jhlkjkljkl"},

        ]
        }

Solution

  • students.courseList.courseNumber[0][0] --> will give 1000000000
    
    students.courseList.courseNumber[0][1] --> will give 1000000001