skip to Main Content
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


  1. Your call jsonPath.getList("products.stock.availabilities.status"); will return a List<List<String>>: Both products and availabilities are Lists. So:

    List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
    
    for (List<String> statuses : listOfProducts) {
        for (String actualProduct : statuses) {
            ...
        }
    }
    
    Login or Signup to reply.
  2. Your status field is within an availabilities array, so when you’re calling getList("products.stock.availabilities.status"), you’re actually getting a list of arrays

    You can flatten this structure by collecting all statuses in one list, like:

    List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
    
    List<String> allStatuses = new ArrayList<>();
    for (List<String> productStatuses : listOfProducts) {
        allStatuses.addAll(productStatuses);
    }
    
    for (String status : allStatuses) {
        // process ..
    }
    
    Login or Signup to reply.
  3. You may try another JSON library Josson.

    https://github.com/octomix/josson

    Deserialization

    Josson josson = Josson.fromJsonString(
        "{" +
        "    "products": [" +
        "        {" +
        "            "id": "1"," +
        "            "product": {" +
        "                "color": "blue"," +
        "                "brand": "Brand"" +
        "            }," +
        "            "stock": {" +
        "                "availabilities": [" +
        "                    {" +
        "                        "status": "IN_STOCK"" +
        "                    }" +
        "                ]" +
        "            }," +
        "            "price": {" +
        "                "total": "1000"," +
        "                "currency": "PLN"" +
        "            }" +
        "        }" +
        "    ]" +
        "}");
    

    Query to Produce JSON Array Node

    String expression = "products.stock.availabilities.status";
    JsonNode array = josson.getNode(expression);
    System.out.println(expression);
    System.out.println(array.toPrettyString());
    for (int i = 0; i < array.size(); i++) {
        System.out.println(array.get(i).asText());
    }
    
    expression = "products.product.color";
    array = josson.getNode(expression);
    System.out.println(expression);
    System.out.println(array.toPrettyString());
    for (int i = 0; i < array.size(); i++) {
        System.out.println(array.get(i).asText());
    }
    

    Output

    products.stock.availabilities.status
    [ "IN_STOCK" ]
    IN_STOCK
    products.product.color
    [ "blue" ]
    blue
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search