JMeter中如何按位置分组生成带数组结构的JSON文件?
Solution to Generate Grouped Nested JSON Files from CSV in JMeter
Prerequisites
- A CSV file with columns:
location,GTIN,approved,erp_stock_RFID_stock(example rows below)location,GTIN,approved,erp_stock_RFID_stock A1,12345,true,10 A1,67890,false,5 B2,11223,true,20 - JMeter (recent versions include Groovy by default, which we’ll use for efficient JSON handling)
Step-by-Step Setup
1. Configure Thread Group
Add a Thread Group to your test plan with these settings:
- Number of Threads: 1
- Loop Count: 1
This ensures we process the CSV exactly once.
2. Add CSV Data Set Config
Right-click Thread Group → Add → Config Element → CSV Data Set Config:
- Filename: Path to your CSV file (e.g.,
./input/your_data.csv) - Variable Names:
location,GTIN,approved,erp_stock_RFID_stock(matches CSV columns) - Recycle on EOF: False
- Stop thread on EOF: True
- Delimiter: Comma (
,) (adjust if your CSV uses a different separator)
3. Add JSR223 Sampler to Accumulate Grouped Data
Right-click Thread Group → Add → Sampler → JSR223 Sampler:
- Set Language to
groovy(critical for JSON support and performance) - Paste this script to build a location-to-products map:
// Initialize or retrieve the location map from JMeter variables def locationMap = vars.getObject("locationMap") ?: new HashMap<>() // Create a product object from current CSV row def product = [ GTIN: vars.get("GTIN"), approved: vars.get("approved").toBoolean(), erp_stock_RFID_stock: vars.get("erp_stock_RFID_stock").toInteger() ] // Add product to the corresponding location's list def productsList = locationMap.getOrDefault(vars.get("location"), []) productsList.add(product) locationMap.put(vars.get("location"), productsList) // Save the updated map back to JMeter variables vars.putObject("locationMap", locationMap)
4. Add JSR223 Listener to Write JSON Files
Right-click Thread Group → Add → Listener → JSR223 Listener:
- Set Language to
groovy - Paste this script to generate and save JSON files after processing all CSV rows:
def locationMap = vars.getObject("locationMap") if (locationMap) { // Create output directory (adjust path as needed) def outputDir = new File("${__P(user.dir)}/output") outputDir.mkdirs() // Generate JSON for each location locationMap.each { location, products -> def json = new groovy.json.JsonBuilder([ location: location, products: products ]).toPrettyString() // Write to file def outputFile = new File(outputDir, "${location}.json") outputFile.write(json, "UTF-8") } }
Example Output JSON
For location A1, the generated file (A1.json) will look like this:
{ "location": "A1", "products": [ { "GTIN": "12345", "approved": true, "erp_stock_RFID_stock": 10 }, { "GTIN": "67890", "approved": false, "erp_stock_RFID_stock": 5 } ] }
Key Tips
- Data Type Adjustments: If your CSV uses
yes/noforapprovedor string values for stock, modify the conversion logic (e.g.,vars.get("approved") == "yes"instead oftoBoolean()). - Permissions: Ensure JMeter has write access to the output directory. Use absolute paths if relative paths don’t work.
- Test First: Validate with a small CSV subset before processing 350 entries to catch errors early.
内容的提问来源于stack exchange,提问作者Sowmya Mohan
相关产品推荐
相关产品推荐

