JsonPath jsonPath = response.jsonPath();
List<String> listOfProducts= jsonPath.getList("products.stock.availabilities.status");
for (int i = 0; i < listOfProducts.size(); i++) {
String actualProduct = listOfProducts.get(i);
...
}
throws class java.util.ArrayList cannot be cast to class java.lang.String
response in this case is:
"products": [
{
"id": "1",
"product": {
"color": "blue",
"brand": "Brand"
},
"stock": {
"availabilities": [
{
"status": "IN_STOCK"
}
]
},
"price": {
"total": "1000",
"currency": "PLN"
}
},
{
..
}
]
For other JSONs it works (where the listOfProducts is actually a List) but for this one I don’t know why it’s ArrayList.
How to either convert the arrayList to List or how to servce both (List and ArrayList depending on the json response)?
EDIT:
I want to have this method common for ArrayLists and for List
The products.stock.availabilities.status
will be a parameter
3
Answers
Your call
jsonPath.getList("products.stock.availabilities.status");
will return aList<List<String>>
: Bothproducts
andavailabilities
are Lists. So:Your
status
field is within anavailabilities
array, so when you’re callinggetList("products.stock.availabilities.status")
, you’re actually getting a list of arraysYou can flatten this structure by collecting all statuses in one list, like:
You may try another JSON library Josson.
https://github.com/octomix/josson
Deserialization
Query to Produce JSON Array Node
Output