Search code examples
javarest-assuredrest-assured-jsonpath

RestAssured JsonPath: How to filter json objects from json


I'm trying to take an array of objects from json file and i have an issue.

path.get("wgcTournaments.items")

What path i should use to get all items(item0, item1, item2 ...) in items?

Can you please give me an advice how to do it.

Json example

{
  "wgcTournaments": {
    "items": {
      "jcr:primaryType": "nt:unstructured",
      "item0": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item1": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item2": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      },
      "item3": {
        "jcr:primaryType": "nt:unstructured",
        "test": "test",
        "test1": "test1"
      }
    }
  }
}

The best way to filter item from items object but i don't understand how to do it with json path.


Solution

  • Finally i found a solution for my question.

    If you want to get item from items you need to use this one json Path

    path.getObject("wgcTournaments.items*.
    find{it.key.startsWith('item')}.value",ItemClass[].class);
    

    Note: it was RestAssured and he uses Gpath more details you can find here http://docs.groovy-lang.org/latest/html/documentation/#_gpath