Gatling JsonPath on variable

59 Views Asked by At

i have an issue with Gatling. i want to do something simple.. lets say i have this Json in this variable and want to do a loadrunner style IDX. Somehow this is not possible??

  1. get the count of where user == same
  2. pick a random number from 0 to the above count
  3. pick the corresponding book_title and put it in a variable
val testjson = """{
  "Books": [
    {
      "book_title": "bookTitle21",
      "user": "same"
    },
    {
      "book_title": "bookTitle73",
      "user": "same"
    },
    {
      "book_title": "bookTitle0",
      "user": "admin"
    }
  ]
}"""

so the outcome would be something like

val userCount = JonPath("$.Books.length").as[Int]

val randomIndex = util.Random.nextInt(userCount)

val randomBook = JsonPath("$.Books[#{randomIndex}].book_title", testjson) //this does not work

see the problem description;

tried alternatives like jmespath and jackson

2

There are 2 best solutions below

1
Amerousful On

If the current json is from a response body, you can use the findRandom method to extract a desired value, save, and then use it through a Session.

    .check(
      jsonPath("$.json.Books..book_title").findRandom.saveAs("randomBook")
    )
0
Forrestman On

I got it although it could be nicer i guess.. used play-json and jmesPath

.check(jmesPath("[?someFilter== 'someBook'].[value, searchKeyValueId]").saveAs("allCombinaties")) .check(jmesPath("length([?someFilter== 'someBook'].[value, searchKeyValueId])").saveAs("allCombinatiesCount"))

    val allCombinatiesCount = session("allCombinatiesCount").as[Int]
    val allecombinaties = session("allCombinaties").as[String]
    val json: JsValue = Json.parse(allecombinaties)
    val randomGekozen = Random.nextInt(allCombinatiesCount)
    val gekozenValue = (json \ randomGekozen).get
    val value = (gekozenValue \ 0).get //get first value
    val searchKeyValueId = (gekozenValue \ 1).get //get second value