How to Make Dynamic Payload Using JSR223 PreProcessor in JMeter
When it comes to realistic performance testing, static payloads just don’t cut it. Imagine testing a hotel reservation system, but every user is booking the exact same room with the same details. That’s not testing — that’s barely a smoke check.
This is where dynamic payloads come into play. And if you’re using Apache JMeter, the JSR223 PreProcessor is best for you. It allows you to craft payloads that adapt — just like real users do — using powerful scripting (Groovy, mainly).
In this blog, I’ll explain the why, the how, and share real code snippets to help you create dynamic payloads that bring your load testing to life.
Why Go Dynamic?
Static payloads are predictable. Real-world traffic isn’t.
Here’s what you get with dynamic payloads:
- Realistic simulations (unique users, rooms, items, etc.)
- Better server behavior testing (like caching, DB locking)
- Scenarios based on loops, data files, or API responses
Dynamic payloads give your test depth.
What is JSR223 PreProcessor?
JSR223 PreProcessor is a component in JMeter that runs just before your HTTP Request. You can write Groovy code (or other languages) to manipulate variables, generate JSONs, or pull data from files or previous responses.
Real Use Case — Dynamic JSON for Hotel Booking API
Let’s say we need to test a hotel reservation API. Each user should book:
- Different room numbers
- Different guest names
- Varying check-in/check-out dates
Let’s get into it.
Step-by-Step: Create a Dynamic JSON Payload
Step 1: Add a JSR223 PreProcessor
- Right-click your HTTP Request > Add > PreProcessors > JSR223 PreProcessor
- Set language to Groovy
Step 2: Prepare Your Variables
groovy
import groovy.json.JsonOutput
// Get loop index to simulate different users
def loopIndex = vars.get(“__jm__ThreadGroup__idx”).toInteger() + 1
// Generate dynamic values
def guestName = “Guest_${loopIndex}”
def checkInDate = “2025–07–01”
def checkOutDate = “2025–07–0” + (loopIndex + 1)
def roomNumber = 100 + loopIndex
// Construct your JSON payload
def payload = [
guestName : guestName,
roomNumber : roomNumber,
checkInDate : checkInDate,
checkOutDate: checkOutDate,
paymentMode : “CreditCard”,
specialNote : “Booking by load test user ${loopIndex}”
]
// Convert to JSON string
def jsonPayload = JsonOutput.toJson(payload)
vars.put(“dynamicPayload”, jsonPayload)
Step 3: Use It in Your HTTP Request
In the Body Data of your HTTP Request sampler, just reference the variable:
json
${dynamicPayload}
Also, don’t forget to set:
- Method: POST
- Content-Type: application/json
Use CSV for Realistic Test Data
You can also use CSV Data Set Config to drive values like names, emails, IDs. Then fetch them in the JSR223 script:
def guestName = vars.get(“csvGuestName”)
def email = vars.get(“csvEmail”)
Now your test is both dynamic and data-driven!
Real-Time Tip: Validate Payload with Debug Sampler
Add a Debug Sampler after your PreProcessor to inspect the generated payload. Helps massively during test design.
- Always wrap your code in try-catch to avoid test halts
- Use vars.put(“yourVar”, value) to share values across samplers
- Use log.info() for debugging during dry runs
Summary
Dynamic payload generation is essential for realistic, scalable, and meaningful performance testing. With the JSR223 PreProcessor and a bit of Groovy scripting, you can:
- Generate user-specific payloads on the fly
- Pull values from loop indices, CSV files, or API responses
- Simulate complex booking, registration, or transaction scenarios
- Create cleaner, smarter, and more powerful test plans
If you’re still using static JSONs in your requests — it’s time to level up.
Originally published at https://www.devstringx.com/what-is-jsr223-preprocessor on July 07, 2025
