Search code examples
jsongatlingscala-gatling

How to feed a value into a field in a json array in Gatling?


I am using Gatling to test an API that accepts a json body like below:

{
 "data": {
      "fields": [
        {
          "rank": 1
        },
        {
          "name": "Jack"
        }
       ]
    }
 }

I have created a file feeder.json that contains array of json objects like above. Below is the feeder.json

[
 {
 "data": {
      "fields": [
        {
          "rank": 1
        },
        {
          "name": "Jack"
        }
       ]
    }
 }
]

I have created another file template.txt that contains the template of above json. Below is the template.txt

{
 "data": {
      "fields": [
         {
          "rank": ${data.fields[0].rank}     //this is not working
        },
        {
          "name": "Jack"
        }
       ]
    }
 }
val jsonFeeder = jsonFile("feeder.json").circular
scenario("Test scenario")
      .feed(jsonFeeder)
      .exec(http("API call test")
       .post("/data")
       .body(ElFileBody("template.txt"))
       .asJson
       .check(status is 200))

I am feeding the feeder.json and also sending json body from template.json. The 'rank' property values should get set from feeder into the json body. But I am getting an error 'Map named 'data' does not contain key 'fields[0]'. Stuck with this.


Solution

  • Access by index syntax uses parens, not square braces.

    #{data.fields(0).rank}