Reputation: 309
I am using rest assured to test my API, I have an array list which contains date which I need to compare with JSON response, but I am not able to get all date values from the response
ArrayList<String> jsonElement = response.path("result[0].data[0].record.date");
I am getting records for the first array object only
Below is my JSON response
{
"statusCode": 200,
"result": [
{
"data": [
{
"record": [
{
"date": "2016-09-07T00:00:00.000000Z"
},
{
"date": "2016-07-07T00:00:00.000000Z"
},
{
"date": "2016-07-21T00:00:00.000000Z"
},
{
"date": "2017-03-31T00:00:00.000000Z"
},
{
"date": "2017-01-30T00:00:00.000000Z"
}
]
},
{
"record": [
{
"date": "2017-07-20T00:00:00.000000Z"
},
{
"date": "2017-08-07T00:00:00.000000Z"
},
{
"date": "2018-01-06T00:00:00.000000Z"
}
]
}
],
"id": "34567890"
}
]
}
Upvotes: 0
Views: 789
Reputation: 2774
I can think of different ways to get this done, but considering result will always be 1 here's a solution
JsonPath js = new JsonPath(json);
ArrayList<String> original = new ArrayList<String>();
int count = js.getInt("result[0].data.size()");
for (int i = 0; i < count; i++) {
original.addAll(js.get("result[0].data[" + i + "].record.date"));
}
System.out.println("Dates : "+original);
System.out.println("Count : "+original.size());
System.out.println("Third Date : "+original.get(2));
Output :
Dates : [2016-09-07T00:00:00.000000Z, 2016-07-07T00:00:00.000000Z, 2016-07-21T00:00:00.000000Z, 2017-03-31T00:00:00.000000Z, 2017-01-30T00:00:00.000000Z, 2017-07-20T00:00:00.000000Z, 2017-08-07T00:00:00.000000Z, 2018-01-06T00:00:00.000000Z]
Count : 8
Third Date : 2016-07-21T00:00:00.000000Z
Upvotes: 2