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

JMeter中修改提取的JSON响应并复用为PUT请求体的方法

Hey there! Let's tackle how to modify that JSON response before sending your PUT request in JMeter—here are a couple of solid, practical approaches you can use:

This is the go-to method because Groovy (the preferred language for JSR223 in JMeter) handles JSON parsing/modification seamlessly, plus it’s more performant than older options like BeanShell.

  1. First, confirm you’ve extracted the full JSON response from your GET request into a variable (e.g., fullJson) using a JSON Extractor or Regular Expression Extractor.
  2. Add a JSR223 PostProcessor directly after your GET request.
  3. Paste this Groovy script into the "Script" field, adjusting the property name and new value to match your needs:
    import groovy.json.JsonSlurper
    import groovy.json.JsonBuilder
    
    // Fetch the extracted JSON string from your variable
    def rawJson = vars.get("fullJson")
    // Parse the string into a manipulable JSON object
    def jsonObject = new JsonSlurper().parseText(rawJson)
    
    // Modify the target property (replace "yourProperty" and "newValue" with your actual data)
    jsonObject.yourProperty = "newValue"
    // For nested properties, use dot notation: e.g., jsonObject.user.profile.email = "updated@example.com"
    
    // Convert the modified object back to a formatted JSON string
    def updatedJson = new JsonBuilder(jsonObject).toPrettyString()
    // Store the updated JSON in a new variable for your PUT request
    vars.put("updatedFullJson", updatedJson)
    
  4. In your PUT request’s "Body Data" section, reference the new variable with ${updatedFullJson}.
Approach 2: JSON Extractor + Manual JSON Construction (For Simple Changes)

If you only need to tweak a few specific fields, this method avoids full JSON parsing:

  1. Use separate JSON Extractor instances to pull out all the fields you need from the GET response (both fields to keep unchanged and the one to modify). For example:
    • Extract id to variable resourceId
    • Extract currentName to variable oldName
    • Extract any other static fields you need
  2. Add a User Defined Variables element and define your new value (e.g., newName=Updated Resource Name).
  3. Add a JSR223 PreProcessor before your PUT request, then build the updated JSON manually:
    def updatedJson = """
    {
      "id": "${vars.get('resourceId')}",
      "name": "${vars.get('newName')}",
      "description": "${vars.get('resourceDescription')}"
    }
    """
    vars.put("updatedFullJson", updatedJson)
    
  4. Reference ${updatedFullJson} in your PUT request’s body as before.
Approach 3: JSON Transform PostProcessor (For Basic, No-Code Edits)

If you’re using JMeter 5.1+, this built-in processor works for simple field replacements:

  1. Add a JSON Transform PostProcessor after your GET request.
  2. In the "JSON Path Expression" field, enter the path to the property you want to change (e.g., $.yourProperty).
  3. In the "Replacement Value" field, input your new value (e.g., "Updated Value").
  4. Set the "Destination Variable Name" to something like updatedJson.
  5. Use ${updatedJson} in your PUT request’s body.

Quick Tips

  • Always use Groovy for JSR223 scripts (select it from the "Script Language" dropdown) — it’s faster and has better JSON support than BeanShell.
  • Use a Debug Sampler + View Results Tree to verify that your modified JSON variable contains the correct data before running the PUT request.
  • If your JSON has special characters (like quotes or newlines), Groovy’s JsonBuilder will handle automatic escaping, so you don’t have to worry about breaking the JSON structure.

内容的提问来源于stack exchange,提问作者Dominik

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:20:55