# Selecting random attribute from list captured via check.(jsonPath("$..attribute").findAll.saveAs

**URL:** <https://community.gatling.io/t/selecting-random-attribute-from-list-captured-via-check-jsonpath-attribute-findall-saveas/2213>\
**Category:** Gatling (Open-Source)\
**Created:** [April 16, 2015, 2:26pm UTC](https://community.gatling.io/t/selecting-random-attribute-from-list-captured-via-check-jsonpath-attribute-findall-saveas/2213 "2015-04-16T14:26:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlastephenwainwright](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mlastephenwainwright](https://community.gatling.io/u/mlastephenwainwright)\
**Post date:** [April 16, 2015, 2:26pm UTC](https://community.gatling.io/t/selecting-random-attribute-from-list-captured-via-check-jsonpath-attribute-findall-saveas/2213/1 "2015-04-16T14:26:42Z")

</div>

Hi,

I have an issue where I when I’ve captured a list of all values returned for 4 attributes in an http response:

.exec(http(“addPosition”)  
.post(baseUrl + “/url/getManagePositionByARVAAjax?organisationId=${userOrgIDParam}&isARAVA=false&screenContext=OWN”)  
.headers(baseHeaders)  
.headers(headers\_21)  
.check(status.is(200))  
.check(jsonPath("$…positionUuid").findAll.saveAs(“positionUuidParamList”))  
.check(jsonPath("$…positionId").findAll.saveAs(“positionIdParamList”))  
.check(jsonPath("$…positionName").findAll.saveAs(“positionNameParamList”))  
.check(jsonPath("$…orgCodeId").findAll.saveAs(“orgCodeIdParamList”))  
)

So say if there are a list of 20 values for each attribute, I want to select randomly, say value 5, for all 4 attributes to use in the next http Post, meaning that I can’t use the ${Attributename.random()} function. How do I do that? Any help would be greatly appreciated.

.exec(http(“createPositionAssignment”)  
.get(baseUrl + “/url/getServerTime?\_=${serverTimestamp}”)  
.headers(headers\_10)  
.resources(http(“createPositionAssignmentRequest”)  
.post(baseUrl + “/url/createPositionAssignmentRequest “)  
.headers(baseHeaders)  
.headers(headers\_21)  
.headers(headers\_19)  
.formParam(“applicantNotes”, “”)  
.formParam(“positionExists”, “true”)  
.formParam(“userPositions”, “[]”)  
.formParam(“userName”, “${firstNameParam} ${lastNameParam}”)  
.formParam(“userUuid”, “${newUserUUID}”)  
.formParam(“workListId”, “${workListIdParam}”)  
.formParam(“workListOrgId”, “”)  
.formParam(“workListOrgCode”, “”)  
.formParam(“workListOrgName”, “”)  
.formParam(“requestType”, “OWN”)  
.formParam(“organizationId”, “${orgIDParam}”)  
.formParam(“changedOrgId”, “”)  
.formParam(“withOutPermission”, “false”)  
.formParam(“proceedWOSponsorApproval”, “true”)  
.formParam(“userPositionVO[0].startDate”, “${dateString}”)  
.formParam(“userPositionVO[0].endDate”, “${dateStringPlusTen}”)  
.formParam(“userPositionVO[0].usersPositionId”, “”)  
.formParam(“userPositionVO[0].positionName”, “${positionNameParam}”)  
.formParam(“userPositionVO[0].positionUuid”, “${positionUuidParam}”)  
.formParam(“userPositionVO[0].positionId”, “${positionIdParam}”)  
.formParam(“userPositionVO[0].orgCode”, “${orgCodeIdParam}”)  
.formParam(“userPositionVO[0].state”, “New”)  
.formParam(“userPositionVO[0].source”, “CareID”)  
.formParam(“userPositionVO[0].managedInId”, “1”)  
.formParam(“userPositionVO[0].organizationId”, “${orgIDParam}”)  
.check(  
regex(“SUCCESS”).find(0).exists  
)  
.check(  
regex(“code”:“SUCCESS”,“message”:”(.\*)”,“category”)  
.saveAs(“requestIdParam”)  
)  
))

Thanks,

Steve

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [April 27, 2015, 8:53am UTC](https://community.gatling.io/t/selecting-random-attribute-from-list-captured-via-check-jsonpath-attribute-findall-saveas/2213/2 "2015-04-27T08:53:23Z")

</div>

The simplest solution is probably to save the whole array in the session, then extract the fields of a random entry manually in a exec(function), like:

.check(jsonPath("$…array").ofType[Seq[Any]].saveAs(“theArray”))

…

.exec { session =\>  
for {  
array ← session(“theArray”).validate[Seq[Map[String, Any]]]  
entry = array(Random.nextInt(array.size))  
} yield session.setAll(“positionUuid” → entry(“positionUuid”), “positionId” → entry(“positionId”))  
}

---

<div class="post-metadata">

**Author:** ![mlastephenwainwright](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mlastephenwainwright](https://community.gatling.io/u/mlastephenwainwright)\
**Post date:** [April 29, 2015, 8:58am UTC](https://community.gatling.io/t/selecting-random-attribute-from-list-captured-via-check-jsonpath-attribute-findall-saveas/2213/3 "2015-04-29T08:58:26Z")

</div>

Thanks Stephane,

That works, although I did have to amend the check slightly:

.check(jsonPath("$[\*]").ofType[Map[String,Any]].findAll.saveAs(“theArray”))

Thanks for your help!

Steve
