You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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/no for approved or string values for stock, modify the conversion logic (e.g., vars.get("approved") == "yes" instead of toBoolean()).
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 09:53:09