Jmeter generate json payload of request dynamically
- Add a Beanshell PreProcessor as a child of the request you want to parametrize
Put following code into the PreProcessor's "Script" area:
StringBuilder result = new StringBuilder();String newline = System.getProperty("line.separator");int max = Integer.parseInt(Parameters);Random random = new Random();result.append("{");result.append("\"productIds\" : [");result.append(newline);for (int i = 1; i < max; i++) { result.append("\"").append(random.nextInt()).append("\","); result.append(newline);}result.append("]");result.append(newline);result.append("}");vars.put("json", result.toString());
- Put your ${productsCount} value into "Parameters" stanza
- Refer generated payload as
${json}
where required
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter.