Have the following scenario, in my JSON response there is a value called `visit` which can have either 1 or more than 1 element. So, if the `visit` have 1 element then the default payload should be sent next POST request or if the `visit` have more than 1 element then I'm fetching a random value and want to update the payload and sent to the next POST request.

Let me elaborate,

**Condition 1** [Have 1 element in the visit section]

[![enter image description here](https://i.stack.imgur.com/oaMSq.png)](https://i.stack.imgur.com/oaMSq.png)

**Condition 2** [Have more than 1 element in the visit section]

[![enter image description here](https://i.stack.imgur.com/GYbtJ.png)](https://i.stack.imgur.com/GYbtJ.png)

**JSON Response for Condition 1**

```
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":201,"siteId":"20000"}],"subjects":[{"studyId":191,"siteRecid":201,"subjectRecid":245,"subjectNumber":"20002"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}

```

**JSON Response for Condition 2**

```
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":16521,"siteId":"11001"}],"subjects":[],"visits":[{"studyId":191,"visitSubmitName":"baseline","visitDisplayName":"Baseline","orderOfDisplay":10},{"studyId":191,"visitSubmitName":"cycle_1","visitDisplayName":"Cycle 1","orderOfDisplay":20}],"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"states":null,"modalities":null,"examDates":null,"series":null,"transferType":null,"customFolder":false,"customFile":false,"folderStructure":null,"fileStructure":null,"allSites":false,"allSubjects":true,"allVisits":false,"allStates":false,"allExamDates":false,"allModalities":false,"allSeries":false,"softEditOverride":false,"includePS":false,"includeSR":false,"includeRTStruct":false,"dicomTemplate":null,"errorMessage":null,"successMessage":null}

```

When I am in Condition 2, fetching random `visitSubmitName`,`visitDisplayName`,`orderOfDisplay` and updating in the payload.

**Payload for condition 1:**

```
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":201,"siteId":"20000"}],"subjects":[{"studyId":191,"siteRecid":201,"subjectRecid":245,"subjectNumber":"20002"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}

```

**Payload for condition 2:**

```
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":16521,"siteId":"11001"}],"allSubjects":true,"states":null,"visits":[{"studyId":191,"visitSubmitName":"baseline","visitDisplayName":"Baseline","orderOfDisplay":10}],"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}

```

Only change in the payload from condition 1 is,

[![enter image description here](https://i.stack.imgur.com/CHdgH.png)](https://i.stack.imgur.com/CHdgH.png)

Solution I have tried so far is created a JSR223 post processer with the following code:

```
import groovy.json.JsonSlurper
Random rnd = new Random();
def jsonString = prev.getResponseDataAsString();
def jsonConvert = new JsonSlurper();
def object = jsonConvert.parseText(jsonString);
def studyCode = object.studyDTO.studyCode;
def visitS = object.visits.size().toString();
def visitSize = visitS?.isInteger() ? visitS.toInteger() : null
def visitNUM = rnd.nextInt(visitSize);
//def defaultPayload = "{"studyDTO":{"studyId":${studyID},"studyCode":"${study_name}"},"sites":[{"studyId":${studyID},"siteRecid":${siteRecID},"siteId":"${siteID}"}],"subjects":[{"studyId":${studyID},"siteRecid":${siteRecID},"subjectRecid":${subjectRecID},"subjectNumber":"${subjectNumber}"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}"

if (visitSize>1) {
    def visitSubmitName = object.visits[visitNUM].visitSubmitName
    def visitDisplayName = object.visits[visitNUM].visitDisplayName
    def orderOfDisplay= object.visits[visitNUM].orderOfDisplay.toString();
    //Change the payload with the three value 

    vars.put('payload',updatedPayload )
}
else {
    vars.put('payload',defaultPayload )
} 

```

And the next post sampler,

[![enter image description here](https://i.stack.imgur.com/bUed4.png)](https://i.stack.imgur.com/bUed4.png)

Error details:

1. Getting error with the `defaultPayload` declaration and need the logic for updating the payload.
2. Is the post sampler declared correctly?

Thanks, in advance