1

In restassured, it's possible to use JsonPath 'find' function to search for object. https://www.javadoc.io/doc/io.rest-assured/json-path/3.0.0/io/restassured/path/json/JsonPath.html

An example given in the link:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

and an example of such search is:

List<Map> books = with(Object).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");

I have a bit more complicated json structure:

enter image description here

and i want to find the json element that has id==100770 (btw, it's unique), i.e. the element located inside 5th element in outer array, and is the 3rd element of inner array

but i don't have a clearly tagged json path like the example above, so what should i use below to replace the xx?

get("xx.xx.find { xx -> xx.id == 100770 }")

could anyone give any suggestion?

user1559625
  • 2,583
  • 5
  • 37
  • 75
  • Did you try: `List elements = with(Object).get("array.contents.findAll { contents-> contents.id== 100770 }");` – Lia Apr 12 '21 at 13:50

1 Answers1

1

You can go for the Groovy path in this type of response traversing

groovy_path = "store.book.find{it.category='reference'}.title"

you can play with the different response keys in place of the category key and title key to get different values from the response.

String titile_of_book = response().extract().jsonPath().getString("groovy_path");

System.out.println(titile_of_book); //This will print ---> Sayings of the Century