title: Gatling - Looping through JSON array
body: <p>I have a block of code which needs to loop through a JSON array which is obtained from response of a REST service. (<a href="https://gist.github.com/neilghosh/7f1ef88e45c44791210f" rel="noreferrer">Full gist available here</a>.)</p>
  
  <pre><code>.exec(http("Request_1")
    .post("/endPoint")
    .headers(headers_1)
    .body(StringBody("""REQUEST_BODY""")).asJSON
    .check(jsonPath("$.result").is("SUCCESS"))
    .check(jsonPath("$.data[*]").findAll.saveAs("pList")))
  .exec(session =&gt; {
    println(session)
    session
  })
  .foreach("${pList}", "player"){
   exec(session =&gt; {
      val playerId = JsonPath.query("$.playerId", "${player}")
      session.set("playerId", playerId)
    })
   .exec(http("Request_1")
      .post("/endPoint")
      .headers(headers_1)
      .body(StringBody("""{"playerId":"${playerId}"}""")).asJSON
      .check(jsonPath("$.result").is("SUCCESS")))
  
  }
  </code></pre>
  
  <p>The response format of the first request  was </p>
  
  <pre><code>{
    "result": "SUCCESS",
    "data": [
      {
        "playerId": 2
      },
      {
        "playerId": 3
      },
      {
        "playerId": 4
      }
    ]
  }
  </code></pre>
  
  <p>And <code>playerId</code> shows up in the session as</p>
  
  <pre><code>pList -&gt; Vector({playerId=2, score=200}, {playerId=3, score=200}
  </code></pre>
  
  <p>I am seeing in the second request the body is </p>
  
  <pre><code>{"playerId":"Right(empty iterator)}
  </code></pre>
  
  <p>Expected : 3  requests with body as </p>
  
  <pre><code> {"playerId":1}
   {"playerId":2}
   {"playerId":3}
  </code></pre>
  
  <p>I can loop over the resulting array successfully if I save just the playerIds: </p>
  
  <pre><code>.check(jsonPath("$.data[*].playerId").findAll.saveAs("pList")))
  </code></pre>