Vraj Patel
1 min readJul 18, 2023

--

To edit a specific JSON attribute to a random string before submitting each request in JMeter, you can use a PreProcessor component called “JSR223 PreProcessor” along with Groovy scripting. Here’s how you can do it:

1. Add a JSR223 PreProcessor:
— Right-click on the HTTP Request sampler, go to Add -> Pre Processors -> JSR223 PreProcessor.

2. Configure the JSR223 PreProcessor:
— Select “groovy” as the Language.
— Use the following script to modify the JSON attribute to a random string:

```groovy
import org.apache.commons.lang3.RandomStringUtils
import groovy.json.JsonSlurper

def json = prev.getSamplerData()
def slurper = new JsonSlurper()
def payload = slurper.parseText(json)

// Modify the JSON attribute to a random string
payload.attributeName = RandomStringUtils.randomAlphanumeric(10)

// Convert the modified payload back to JSON
def modifiedJson = JsonOutput.toJson(payload)

// Update the request body with the modified JSON
sampler.getArguments().getArgument(0).setValue(modifiedJson)
```

Make sure to replace `attributeName` with the actual name of the JSON attribute you want to modify, and adjust the `RandomStringUtils.randomAlphanumeric(10)` part if you want a different length or type of random string.

3. Save the Test Plan and run the stress test as described in the previous response.

With this configuration, the JSR223 PreProcessor will execute before each request, modifying the specified JSON attribute to a random string.

--

--